lawrencepit-machinery 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +54 -7
- data/lib/machinery.rb +2 -1
- metadata +1 -1
data/README.markdown
CHANGED
@@ -20,6 +20,8 @@ A DataMapper abstraction should be too easy.
|
|
20
20
|
Usage
|
21
21
|
-----
|
22
22
|
|
23
|
+
#### Object Graphs
|
24
|
+
|
23
25
|
In /spec/scenarios.rb
|
24
26
|
|
25
27
|
Machinery.define do
|
@@ -61,16 +63,61 @@ In /spec/models/ship_spec.rb
|
|
61
63
|
end
|
62
64
|
|
63
65
|
|
64
|
-
|
65
|
-
|
66
|
+
#### Just the speed up please
|
67
|
+
|
68
|
+
If it's only speeding up your tests you're after you can use anonymous scenarios, like this:
|
69
|
+
|
70
|
+
In /spec/models/ship_spec.rb
|
71
|
+
|
72
|
+
describe Ship do
|
73
|
+
scenario do
|
74
|
+
@pirate = Ship.create!(:name => "Pirate")
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should update a pirate ship" do
|
78
|
+
@pirate.name = "Updated Pirate"
|
79
|
+
@pirate.save
|
80
|
+
@pirate.reload
|
81
|
+
@pirate.name.should == "Updated Pirate"
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should have 1 ship" do
|
85
|
+
Ship.count.should == 1
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should be named 'Pirate'" do
|
89
|
+
@pirate.name.should == "Pirate"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
This will create one ship only in the database.
|
94
|
+
|
66
95
|
|
67
|
-
When you use +scenario+ with a block you are defining a scenario. Any instance variables you declare within a scenario will be available in your test.
|
68
96
|
|
69
|
-
|
97
|
+
Explain
|
98
|
+
-------
|
70
99
|
|
71
|
-
When you
|
72
|
-
|
73
|
-
|
100
|
+
When you use +scenario+ with a block you are defining a scenario. Any instance
|
101
|
+
variables you declare within a scenario will be available in your test when you
|
102
|
+
use that scenario.
|
103
|
+
|
104
|
+
When you use +scenario+ without a block then you indicate that the block for the
|
105
|
+
scenario you previously defined should be executed. Any instance variables you
|
106
|
+
defined within the block will now be available to you in your test methods. Note
|
107
|
+
that you can re-use scenarios within scenarios.
|
108
|
+
|
109
|
+
When you declare to use a scenario within an rspec +ExampleGroup+ (as in the first
|
110
|
+
ship_spec example above) then the instances will be created for you exactly once.
|
111
|
+
They will automatically be removed from the database after all the tests have run.
|
112
|
+
Any instance variables declared within a scenario will be cloned before each test.
|
113
|
+
If you make sure you use +use_transactional_fixtures+ then this means:
|
114
|
+
* speedy tests, because instances are created exactly once for all tests
|
115
|
+
* you can mess around with the instance variables any way you like within a test method as before each test they will always be in exactly the same state.
|
116
|
+
|
117
|
+
A special case is when you use an anonymous +scenario+ with a block, specified at the
|
118
|
+
top of your tests (as in the second ship_spec example above). It will behave as if it's
|
119
|
+
a named scenario *and* it is used immediately.
|
120
|
+
At most one anonymous +scenario+ per example group can be used.
|
74
121
|
|
75
122
|
|
76
123
|
Magic?
|
data/lib/machinery.rb
CHANGED
@@ -50,7 +50,8 @@ module Machinery
|
|
50
50
|
|
51
51
|
module ExampleGroupMacros
|
52
52
|
def scenario(*names, &block)
|
53
|
-
|
53
|
+
scenario(:__anonymous__, &block) and scenario(:__anonymous__) and return if names.empty?
|
54
|
+
Machinery.scenario(names.first, &block) and return true if block_given?
|
54
55
|
self.before(:all) { scenarios(*names) }
|
55
56
|
self.after(:all) { rollback_scenarios }
|
56
57
|
self.before(:each) { clone_instance_variables }
|