lawrencepit-machinery 0.5 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +53 -27
- metadata +1 -1
data/README.markdown
CHANGED
@@ -3,19 +3,18 @@ Machinery
|
|
3
3
|
|
4
4
|
*Machinery to create object graphs.*
|
5
5
|
|
6
|
-
Machinery helps to create object graphs for use in tests without needing
|
7
|
-
These object graphs are easily re-usable
|
8
|
-
in rake tasks to populate a database.
|
6
|
+
Machinery helps to create object graphs for use in tests without needing
|
7
|
+
fixtures, using pure ruby style. These object graphs are easily re-usable
|
8
|
+
in Test::Unit, RSpec, Cucumber and in rake tasks to populate a database.
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
@user_admin.should be_nil
|
10
|
+
Secondly, even if you don't need object graphs, Machinery can speed up
|
11
|
+
your tests significantly because you can set up any model objects in
|
12
|
+
a before(:all) instead of a before(:each), and at the end of the tests
|
13
|
+
all activerecord objects created in the before(:all) will be cleaned up
|
14
|
+
automatically.
|
17
15
|
|
18
|
-
|
16
|
+
Machinery currently only works with ActiveRecord.
|
17
|
+
A DataMapper abstraction should be too easy.
|
19
18
|
|
20
19
|
|
21
20
|
Usage
|
@@ -23,26 +22,28 @@ Usage
|
|
23
22
|
|
24
23
|
In /spec/scenarios.rb
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
Machinery.define do
|
26
|
+
scenario :earth_ships do
|
27
|
+
@titanic = Ship.create!(:name => "Titanic")
|
28
|
+
@pirate = Ship.create!(:name => "Pirate")
|
29
|
+
end
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
scenario :space_ships do
|
32
|
+
@apollo = Ship.create!(:name => "Apollo")
|
33
|
+
@uss_enterprise = Ship.create!(:name => "USS Enterprise")
|
34
|
+
end
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
scenario :all_ships do
|
37
|
+
scenarios :earth_ships, :space_ships
|
38
|
+
@sunken_ship = Ship.create!(:name => "Sunk")
|
39
|
+
end
|
39
40
|
end
|
40
41
|
|
41
42
|
|
42
43
|
In /spec/spec_helper.rb
|
43
44
|
|
44
45
|
require 'machinery'
|
45
|
-
|
46
|
+
require File.join(File.dirname(__FILE__) + 'scenarios')
|
46
47
|
|
47
48
|
|
48
49
|
In /spec/models/ship_spec.rb
|
@@ -50,8 +51,8 @@ In /spec/models/ship_spec.rb
|
|
50
51
|
describe Ship do
|
51
52
|
scenarios :all_ships
|
52
53
|
|
53
|
-
it "should create a
|
54
|
-
@
|
54
|
+
it "should create a pirate ship" do
|
55
|
+
@pirate.should_not be_nil
|
55
56
|
end
|
56
57
|
|
57
58
|
it "should have 5 ships" do
|
@@ -60,6 +61,31 @@ In /spec/models/ship_spec.rb
|
|
60
61
|
end
|
61
62
|
|
62
63
|
|
64
|
+
Explain
|
65
|
+
-------
|
66
|
+
|
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
|
+
|
69
|
+
When you use +scenario+ without a block then you indicate that the block for the scenario you previously defined should be executed. Note that you can re-use scenarios within scenarios.
|
70
|
+
|
71
|
+
When you declare to use a scenario within an rspec +ExampleGroup+ (as in the example above) then the instances will be created for you exactly once. They will automatically be removed from the database after all the tests have run. Any instance variables declared within a scenario will be cloned before each test. If you make sure you use +use_transactional_fixtures+ then this means:
|
72
|
+
- speedy tests, because instances are created exactly once for all tests
|
73
|
+
- 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.
|
74
|
+
|
75
|
+
|
76
|
+
Magic?
|
77
|
+
------
|
78
|
+
|
79
|
+
Why write stuff like:
|
80
|
+
|
81
|
+
users(:admin).should be_nil
|
82
|
+
|
83
|
+
when you can just as well write:
|
84
|
+
|
85
|
+
@user_admin.should be_nil
|
86
|
+
|
87
|
+
Use less magic. Less code. Makes software softer.
|
88
|
+
|
63
89
|
|
64
90
|
Installation
|
65
91
|
------------
|
@@ -122,9 +148,9 @@ for inspiration.
|
|
122
148
|
|
123
149
|
After using model_stubbing on a relatively large project for over a year I decided
|
124
150
|
a simpler solution was needed. Object graphs need to be easily re-usable by specs,
|
125
|
-
unit tests, cucumber stories and ad-hoc database loading.
|
151
|
+
unit tests, cucumber stories and ad-hoc database loading. And tests need to be speedy.
|
126
152
|
|
127
|
-
Machinery works especially
|
153
|
+
Machinery works especially pleasurable in combination with
|
128
154
|
[Machinist](http://github.com/notahat/machinist) or
|
129
155
|
[FactoryGirl](http://github.com/thoughtbot/factory_girl).
|
130
156
|
|