thoughtbot-factory_girl 1.1.5 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +228 -0
- data/Rakefile +7 -5
- data/lib/factory_girl.rb +11 -4
- data/lib/factory_girl/aliases.rb +18 -7
- data/lib/factory_girl/attribute.rb +10 -20
- data/lib/factory_girl/attribute/association.rb +18 -0
- data/lib/factory_girl/attribute/dynamic.rb +17 -0
- data/lib/factory_girl/attribute/static.rb +17 -0
- data/lib/factory_girl/factory.rb +199 -119
- data/lib/factory_girl/proxy.rb +62 -0
- data/lib/factory_girl/proxy/attributes_for.rb +21 -0
- data/lib/factory_girl/proxy/build.rb +29 -0
- data/lib/factory_girl/proxy/create.rb +10 -0
- data/lib/factory_girl/proxy/stub.rb +28 -0
- data/lib/factory_girl/sequence.rb +2 -0
- data/lib/factory_girl/syntax.rb +12 -0
- data/lib/factory_girl/syntax/blueprint.rb +42 -0
- data/lib/factory_girl/syntax/generate.rb +68 -0
- data/lib/factory_girl/syntax/make.rb +39 -0
- data/lib/factory_girl/syntax/sham.rb +42 -0
- data/test/aliases_test.rb +1 -1
- data/test/association_attribute_test.rb +31 -0
- data/test/attribute_test.rb +8 -46
- data/test/attributes_for_strategy_test.rb +55 -0
- data/test/build_strategy_test.rb +79 -0
- data/test/create_strategy_test.rb +90 -0
- data/test/dynamic_attribute_test.rb +41 -0
- data/test/factory_test.rb +255 -243
- data/test/integration_test.rb +91 -3
- data/test/models.rb +1 -0
- data/test/sequence_test.rb +1 -1
- data/test/static_attribute_test.rb +33 -0
- data/test/strategy_test.rb +33 -0
- data/test/stub_strategy_test.rb +52 -0
- data/test/syntax/blueprint_test.rb +39 -0
- data/test/syntax/generate_test.rb +63 -0
- data/test/syntax/make_test.rb +39 -0
- data/test/syntax/sham_test.rb +40 -0
- data/test/test_helper.rb +1 -0
- metadata +42 -8
- data/README.textile +0 -151
- data/lib/factory_girl/attribute_proxy.rb +0 -92
- data/test/attribute_proxy_test.rb +0 -121
data/test/integration_test.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'test_helper'
|
2
2
|
|
3
3
|
class IntegrationTest < Test::Unit::TestCase
|
4
4
|
|
@@ -10,7 +10,7 @@ class IntegrationTest < Test::Unit::TestCase
|
|
10
10
|
f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
|
11
11
|
end
|
12
12
|
|
13
|
-
Factory.define Post do |f|
|
13
|
+
Factory.define Post, :default_strategy => :attributes_for do |f|
|
14
14
|
f.name 'Test Post'
|
15
15
|
f.association :author, :factory => :user
|
16
16
|
end
|
@@ -19,9 +19,15 @@ class IntegrationTest < Test::Unit::TestCase
|
|
19
19
|
f.first_name 'Ben'
|
20
20
|
f.last_name 'Stein'
|
21
21
|
f.admin true
|
22
|
+
f.sequence(:username) { |n| "username#{n}" }
|
22
23
|
f.email { Factory.next(:email) }
|
23
24
|
end
|
24
|
-
|
25
|
+
|
26
|
+
Factory.define :guest, :parent => :user do |f|
|
27
|
+
f.last_name 'Anonymous'
|
28
|
+
f.username 'GuestUser'
|
29
|
+
end
|
30
|
+
|
25
31
|
Factory.sequence :email do |n|
|
26
32
|
"somebody#{n}@example.com"
|
27
33
|
end
|
@@ -99,7 +105,33 @@ class IntegrationTest < Test::Unit::TestCase
|
|
99
105
|
end
|
100
106
|
|
101
107
|
end
|
108
|
+
|
109
|
+
context "a generated mock instance" do
|
110
|
+
|
111
|
+
setup do
|
112
|
+
@stub = Factory.stub(:user, :first_name => 'Bill')
|
113
|
+
end
|
114
|
+
|
115
|
+
should "assign all attributes" do
|
116
|
+
[:admin, :email, :first_name, :last_name].each do |attr|
|
117
|
+
assert_not_nil @stub.send(attr)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
should "correctly assign lazy, dependent attributes" do
|
122
|
+
assert_equal "bill.hendrix@example.com", @stub.email
|
123
|
+
end
|
102
124
|
|
125
|
+
should "override attrbutes" do
|
126
|
+
assert_equal 'Bill', @stub.first_name
|
127
|
+
end
|
128
|
+
|
129
|
+
should "not assign associations" do
|
130
|
+
assert_nil Factory.stub(:post).author
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
|
103
135
|
context "an instance generated by a factory with a custom class name" do
|
104
136
|
|
105
137
|
setup do
|
@@ -115,6 +147,28 @@ class IntegrationTest < Test::Unit::TestCase
|
|
115
147
|
end
|
116
148
|
|
117
149
|
end
|
150
|
+
|
151
|
+
context "an instance generated by a factory that inherits from another factory" do
|
152
|
+
setup do
|
153
|
+
@instance = Factory.create(:guest)
|
154
|
+
end
|
155
|
+
|
156
|
+
should "use the class name of the parent factory" do
|
157
|
+
assert_kind_of User, @instance
|
158
|
+
end
|
159
|
+
|
160
|
+
should "have attributes of the parent" do
|
161
|
+
assert_equal 'Jimi', @instance.first_name
|
162
|
+
end
|
163
|
+
|
164
|
+
should "have attributes defined in the factory itself" do
|
165
|
+
assert_equal 'GuestUser', @instance.username
|
166
|
+
end
|
167
|
+
|
168
|
+
should "have attributes that have been overriden" do
|
169
|
+
assert_equal 'Anonymous', @instance.last_name
|
170
|
+
end
|
171
|
+
end
|
118
172
|
|
119
173
|
context "an attribute generated by a sequence" do
|
120
174
|
|
@@ -143,5 +197,39 @@ class IntegrationTest < Test::Unit::TestCase
|
|
143
197
|
end
|
144
198
|
|
145
199
|
end
|
200
|
+
|
201
|
+
context "an attribute generated by an in-line sequence" do
|
202
|
+
|
203
|
+
setup do
|
204
|
+
@username = Factory.attributes_for(:admin)[:username]
|
205
|
+
end
|
206
|
+
|
207
|
+
should "match the correct format" do
|
208
|
+
assert_match /^username\d+$/, @username
|
209
|
+
end
|
210
|
+
|
211
|
+
context "after the attribute has already been generated once" do
|
212
|
+
|
213
|
+
setup do
|
214
|
+
@another_username = Factory.attributes_for(:admin)[:username]
|
215
|
+
end
|
216
|
+
|
217
|
+
should "match the correct format" do
|
218
|
+
assert_match /^username\d+$/, @username
|
219
|
+
end
|
220
|
+
|
221
|
+
should "not be the same as the first generated value" do
|
222
|
+
assert_not_equal @username, @another_username
|
223
|
+
end
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
context "a factory with a default strategy specified" do
|
230
|
+
should "generate instances according to the strategy" do
|
231
|
+
assert_kind_of Hash, Factory(:post)
|
232
|
+
end
|
233
|
+
end
|
146
234
|
|
147
235
|
end
|
data/test/models.rb
CHANGED
data/test/sequence_test.rb
CHANGED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class StaticAttributeTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "a static attribute" do
|
6
|
+
setup do
|
7
|
+
@name = :first_name
|
8
|
+
@value = 'John'
|
9
|
+
@attr = Factory::Attribute::Static.new(@name, @value)
|
10
|
+
end
|
11
|
+
|
12
|
+
should "have a name" do
|
13
|
+
assert_equal @name, @attr.name
|
14
|
+
end
|
15
|
+
|
16
|
+
should "set its static value on a proxy" do
|
17
|
+
@proxy = mock('proxy')
|
18
|
+
@proxy.expects(:set).with(@name, @value)
|
19
|
+
@attr.add_to(@proxy)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
should "raise an error when defining an attribute writer" do
|
24
|
+
assert_raise Factory::AttributeDefinitionError do
|
25
|
+
Factory::Attribute::Static.new('test=', nil)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
should "convert names to symbols" do
|
30
|
+
assert_equal :name, Factory::Attribute::Static.new('name', nil).name
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class Proxy < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "a proxy" do
|
6
|
+
setup do
|
7
|
+
@proxy = Factory::Proxy.new(Class.new)
|
8
|
+
end
|
9
|
+
|
10
|
+
should "do nothing when asked to set an attribute to a value" do
|
11
|
+
assert_nothing_raised { @proxy.set(:name, 'a name') }
|
12
|
+
end
|
13
|
+
|
14
|
+
should "return nil when asked for an attribute" do
|
15
|
+
assert_nil @proxy.get(:name)
|
16
|
+
end
|
17
|
+
|
18
|
+
should "call get for a missing method" do
|
19
|
+
@proxy.expects(:get).with(:name).returns("it's a name")
|
20
|
+
assert_equal "it's a name", @proxy.name
|
21
|
+
end
|
22
|
+
|
23
|
+
should "do nothing when asked to associate with another factory" do
|
24
|
+
assert_nothing_raised { @proxy.associate(:owner, :user, {}) }
|
25
|
+
end
|
26
|
+
|
27
|
+
should "raise an error when asked for the result" do
|
28
|
+
assert_raise(NotImplementedError) { @proxy.result }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class StubProxyTest < Test::Unit::TestCase
|
4
|
+
context "the stub proxy" do
|
5
|
+
setup do
|
6
|
+
@proxy = Factory::Proxy::Stub.new(@class)
|
7
|
+
end
|
8
|
+
|
9
|
+
context "when asked to associate with another factory" do
|
10
|
+
setup do
|
11
|
+
Factory.stubs(:create)
|
12
|
+
@proxy.associate(:owner, :user, {})
|
13
|
+
end
|
14
|
+
|
15
|
+
should "not set a value for the association" do
|
16
|
+
assert_nil @proxy.result.owner
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
should "return nil when building an association" do
|
21
|
+
assert_nil @proxy.association(:user)
|
22
|
+
end
|
23
|
+
|
24
|
+
should "not call Factory.create when building an association" do
|
25
|
+
Factory.expects(:create).never
|
26
|
+
assert_nil @proxy.association(:user)
|
27
|
+
end
|
28
|
+
|
29
|
+
should "always return nil when building an association" do
|
30
|
+
@proxy.set(:association, 'x')
|
31
|
+
assert_nil @proxy.association(:user)
|
32
|
+
end
|
33
|
+
|
34
|
+
should "return a mock object when asked for the result" do
|
35
|
+
assert_kind_of Object, @proxy.result
|
36
|
+
end
|
37
|
+
|
38
|
+
context "after setting an attribute" do
|
39
|
+
setup do
|
40
|
+
@proxy.set(:attribute, 'value')
|
41
|
+
end
|
42
|
+
|
43
|
+
should "add a stub to the resulting object" do
|
44
|
+
assert_equal 'value', @proxy.attribute
|
45
|
+
end
|
46
|
+
|
47
|
+
should "return that value when asked for that attribute" do
|
48
|
+
assert_equal 'value', @proxy.get(:attribute)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require 'factory_girl/syntax/blueprint'
|
4
|
+
|
5
|
+
class BlueprintSyntaxTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
context "a blueprint" do
|
8
|
+
setup do
|
9
|
+
Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
|
10
|
+
User.blueprint do
|
11
|
+
first_name { 'Bill' }
|
12
|
+
last_name { 'Nye' }
|
13
|
+
email { Factory.next(:email) }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
teardown do
|
18
|
+
Factory.factories.clear
|
19
|
+
Factory.sequences.clear
|
20
|
+
end
|
21
|
+
|
22
|
+
context "after making an instance" do
|
23
|
+
setup do
|
24
|
+
@instance = Factory(:user, :last_name => 'Rye')
|
25
|
+
end
|
26
|
+
|
27
|
+
should "use attributes from the blueprint" do
|
28
|
+
assert_equal 'Bill', @instance.first_name
|
29
|
+
end
|
30
|
+
|
31
|
+
should "evaluate attribute blocks for each instance" do
|
32
|
+
assert_match /somebody\d+@example.com/, @instance.email
|
33
|
+
assert_not_equal @instance.email, Factory(:user).email
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require 'factory_girl/syntax/generate'
|
4
|
+
|
5
|
+
class GenerateSyntaxTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
context "a factory" do
|
8
|
+
setup do
|
9
|
+
Factory.define :user do |factory|
|
10
|
+
factory.first_name 'Bill'
|
11
|
+
factory.last_name 'Nye'
|
12
|
+
factory.email 'science@guys.net'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
teardown do
|
17
|
+
Factory.factories.clear
|
18
|
+
end
|
19
|
+
|
20
|
+
should "not raise an error when generating an invalid instance" do
|
21
|
+
assert_nothing_raised { User.generate(:first_name => nil) }
|
22
|
+
end
|
23
|
+
|
24
|
+
should "raise an error when forcefully generating an invalid instance" do
|
25
|
+
assert_raise ActiveRecord::RecordInvalid do
|
26
|
+
User.generate!(:first_name => nil)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
%w(generate generate! spawn).each do |method|
|
31
|
+
should "yield a generated instance when using #{method} with a block" do
|
32
|
+
instance = nil
|
33
|
+
User.send(method) {|instance| }
|
34
|
+
assert_kind_of User, instance
|
35
|
+
end
|
36
|
+
|
37
|
+
context "after generating an instance using #{method}" do
|
38
|
+
setup do
|
39
|
+
@instance = User.send(method, :last_name => 'Rye')
|
40
|
+
end
|
41
|
+
|
42
|
+
should "use attributes from the factory" do
|
43
|
+
assert_equal 'Bill', @instance.first_name
|
44
|
+
end
|
45
|
+
|
46
|
+
should "use attributes passed to generate" do
|
47
|
+
assert_equal 'Rye', @instance.last_name
|
48
|
+
end
|
49
|
+
|
50
|
+
if method == 'spawn'
|
51
|
+
should "not save the record" do
|
52
|
+
assert @instance.new_record?
|
53
|
+
end
|
54
|
+
else
|
55
|
+
should "save the record" do
|
56
|
+
assert !@instance.new_record?
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require 'factory_girl/syntax/make'
|
4
|
+
|
5
|
+
class MakeSyntaxTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
context "a factory" do
|
8
|
+
setup do
|
9
|
+
Factory.define :user do |factory|
|
10
|
+
factory.first_name 'Bill'
|
11
|
+
factory.last_name 'Nye'
|
12
|
+
factory.email 'science@guys.net'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
teardown do
|
17
|
+
Factory.factories.clear
|
18
|
+
end
|
19
|
+
|
20
|
+
context "after making an instance" do
|
21
|
+
setup do
|
22
|
+
@instance = User.make(:last_name => 'Rye')
|
23
|
+
end
|
24
|
+
|
25
|
+
should "use attributes from the factory" do
|
26
|
+
assert_equal 'Bill', @instance.first_name
|
27
|
+
end
|
28
|
+
|
29
|
+
should "use attributes passed to make" do
|
30
|
+
assert_equal 'Rye', @instance.last_name
|
31
|
+
end
|
32
|
+
|
33
|
+
should "save the record" do
|
34
|
+
assert !@instance.new_record?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
require 'factory_girl/syntax/sham'
|
4
|
+
|
5
|
+
class ShamSyntaxTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
context "a factory" do
|
8
|
+
setup do
|
9
|
+
Sham.name { "Name" }
|
10
|
+
Sham.email { "somebody#{rand(5)}@example.com" }
|
11
|
+
|
12
|
+
Factory.define :user do |factory|
|
13
|
+
factory.first_name { Sham.name }
|
14
|
+
factory.last_name { Sham.name }
|
15
|
+
factory.email { Sham.email }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
teardown do
|
20
|
+
Factory.factories.clear
|
21
|
+
Factory.sequences.clear
|
22
|
+
end
|
23
|
+
|
24
|
+
context "after making an instance" do
|
25
|
+
setup do
|
26
|
+
@instance = Factory(:user, :last_name => 'Rye')
|
27
|
+
end
|
28
|
+
|
29
|
+
should "support a sham called 'name'" do
|
30
|
+
assert_equal 'Name', @instance.first_name
|
31
|
+
end
|
32
|
+
|
33
|
+
should "use the sham for the email" do
|
34
|
+
assert_match /somebody\d@example.com/, @instance.email
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|