freegenie-factory_girl 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +9 -0
  2. data/Changelog +29 -0
  3. data/LICENSE +19 -0
  4. data/README.rdoc +236 -0
  5. data/Rakefile +81 -0
  6. data/lib/factory_girl/aliases.rb +50 -0
  7. data/lib/factory_girl/attribute/association.rb +20 -0
  8. data/lib/factory_girl/attribute/dynamic.rb +20 -0
  9. data/lib/factory_girl/attribute/static.rb +17 -0
  10. data/lib/factory_girl/attribute.rb +29 -0
  11. data/lib/factory_girl/factory.rb +412 -0
  12. data/lib/factory_girl/proxy/attributes_for.rb +21 -0
  13. data/lib/factory_girl/proxy/build.rb +29 -0
  14. data/lib/factory_girl/proxy/create.rb +10 -0
  15. data/lib/factory_girl/proxy/stub.rb +49 -0
  16. data/lib/factory_girl/proxy.rb +62 -0
  17. data/lib/factory_girl/sequence.rb +63 -0
  18. data/lib/factory_girl/syntax/blueprint.rb +42 -0
  19. data/lib/factory_girl/syntax/generate.rb +68 -0
  20. data/lib/factory_girl/syntax/make.rb +39 -0
  21. data/lib/factory_girl/syntax/sham.rb +42 -0
  22. data/lib/factory_girl/syntax.rb +12 -0
  23. data/lib/factory_girl.rb +39 -0
  24. data/spec/factory_girl/aliases_spec.rb +29 -0
  25. data/spec/factory_girl/attribute/association_spec.rb +29 -0
  26. data/spec/factory_girl/attribute/dynamic_spec.rb +49 -0
  27. data/spec/factory_girl/attribute/static_spec.rb +29 -0
  28. data/spec/factory_girl/attribute_spec.rb +30 -0
  29. data/spec/factory_girl/factory_spec.rb +525 -0
  30. data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
  31. data/spec/factory_girl/proxy/build_spec.rb +73 -0
  32. data/spec/factory_girl/proxy/create_spec.rb +83 -0
  33. data/spec/factory_girl/proxy/stub_spec.rb +69 -0
  34. data/spec/factory_girl/proxy_spec.rb +28 -0
  35. data/spec/factory_girl/sequence_spec.rb +66 -0
  36. data/spec/factory_girl/syntax/blueprint_spec.rb +35 -0
  37. data/spec/factory_girl/syntax/generate_spec.rb +57 -0
  38. data/spec/factory_girl/syntax/make_spec.rb +35 -0
  39. data/spec/factory_girl/syntax/sham_spec.rb +35 -0
  40. data/spec/integration_spec.rb +265 -0
  41. data/spec/models.rb +43 -0
  42. data/spec/spec_helper.rb +18 -0
  43. data/test/factory_girl_test.rb +28 -0
  44. metadata +118 -0
@@ -0,0 +1,83 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ describe Factory::Proxy::Create do
4
+ before do
5
+ @class = Class.new
6
+ @instance = "built-instance"
7
+ @association = "associated-instance"
8
+
9
+ stub(@class).new { @instance }
10
+ stub(Factory).create { @association }
11
+ stub(@instance).attribute { 'value' }
12
+ stub(@instance, :attribute=)
13
+ stub(@instance, :owner=)
14
+ stub(@instance).save!
15
+
16
+ @proxy = Factory::Proxy::Create.new(@class)
17
+ end
18
+
19
+ it "should instantiate the class" do
20
+ @class.should have_received.new
21
+ end
22
+
23
+ describe "when asked to associate with another factory" do
24
+ before do
25
+ @proxy.associate(:owner, :user, {})
26
+ end
27
+
28
+ it "should create the associated instance" do
29
+ Factory.should have_received.create(:user, {})
30
+ end
31
+
32
+ it "should set the associated instance" do
33
+ @instance.should have_received.method_missing(:owner=, @association)
34
+ end
35
+ end
36
+
37
+ it "should call Factory.create when building an association" do
38
+ association = 'association'
39
+ attribs = { :first_name => 'Billy' }
40
+ stub(Factory).create { association }
41
+ @proxy.association(:user, attribs).should == association
42
+ Factory.should have_received.create(:user, attribs)
43
+ end
44
+
45
+ describe "when asked for the result" do
46
+ before do
47
+ @result = @proxy.result
48
+ end
49
+
50
+ it "should save the instance" do
51
+ @instance.should have_received.save!
52
+ end
53
+
54
+ it "should return the built instance" do
55
+ @result.should == @instance
56
+ end
57
+ end
58
+
59
+ describe "when setting an attribute" do
60
+ before do
61
+ @proxy.set(:attribute, 'value')
62
+ end
63
+
64
+ it "should set that value" do
65
+ @instance.should have_received.method_missing(:attribute=, 'value')
66
+ end
67
+ end
68
+
69
+ describe "when getting an attribute" do
70
+ before do
71
+ @result = @proxy.get(:attribute)
72
+ end
73
+
74
+ it "should ask the built class for the value" do
75
+ @instance.should have_received.attribute
76
+ end
77
+
78
+ it "should return the value for that attribute" do
79
+ @result.should == 'value'
80
+ end
81
+ end
82
+ end
83
+
@@ -0,0 +1,69 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ describe Factory::Proxy::Stub do
4
+ before do
5
+ @class = "class"
6
+ @instance = "instance"
7
+ stub(@class).new { @instance }
8
+ stub(@instance, :id=)
9
+ stub(@instance).id { 42 }
10
+ stub(@instance).reload { @instance.connection.reload }
11
+
12
+ @stub = Factory::Proxy::Stub.new(@class)
13
+ end
14
+
15
+ it "should not be a new record" do
16
+ @stub.result.should_not be_new_record
17
+ end
18
+
19
+ it "should not be able to connect to the database" do
20
+ lambda { @stub.result.reload }.should raise_error(RuntimeError)
21
+ end
22
+
23
+ describe "when a user factory exists" do
24
+ before do
25
+ @user = "user"
26
+ stub(Factory).stub(:user, {}) { @user }
27
+ end
28
+
29
+ describe "when asked to associate with another factory" do
30
+ before do
31
+ stub(@instance).owner { @user }
32
+ mock(Factory).stub(:user, {}) { @user }
33
+ mock(@stub).set(:owner, @user)
34
+
35
+ @stub.associate(:owner, :user, {})
36
+ end
37
+
38
+ it "should set a value for the association" do
39
+ @stub.result.owner.should == @user
40
+ end
41
+ end
42
+
43
+ it "should return the association when building one" do
44
+ mock(Factory).create.never
45
+ @stub.association(:user).should == @user
46
+ end
47
+
48
+ it "should return the actual instance when asked for the result" do
49
+ @stub.result.should == @instance
50
+ end
51
+ end
52
+
53
+ describe "with an existing attribute" do
54
+ before do
55
+ @value = "value"
56
+ mock(@instance).send(:attribute) { @value }
57
+ mock(@instance).send(:attribute=, @value)
58
+ @stub.set(:attribute, @value)
59
+ end
60
+
61
+ it "should to the resulting object" do
62
+ @stub.attribute.should == 'value'
63
+ end
64
+
65
+ it "should return that value when asked for that attribute" do
66
+ @stub.get(:attribute).should == @value
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe Factory::Proxy do
4
+ before do
5
+ @proxy = Factory::Proxy.new(Class.new)
6
+ end
7
+
8
+ it "should do nothing when asked to set an attribute to a value" do
9
+ lambda { @proxy.set(:name, 'a name') }.should_not raise_error
10
+ end
11
+
12
+ it "should return nil when asked for an attribute" do
13
+ @proxy.get(:name).should be_nil
14
+ end
15
+
16
+ it "should call get for a missing method" do
17
+ mock(@proxy).get(:name) { "it's a name" }
18
+ @proxy.name.should == "it's a name"
19
+ end
20
+
21
+ it "should do nothing when asked to associate with another factory" do
22
+ lambda { @proxy.associate(:owner, :user, {}) }.should_not raise_error
23
+ end
24
+
25
+ it "should raise an error when asked for the result" do
26
+ lambda { @proxy.result }.should raise_error(NotImplementedError)
27
+ end
28
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe Factory::Sequence do
4
+ describe "a sequence" do
5
+ before do
6
+ @sequence = Factory::Sequence.new {|n| "=#{n}" }
7
+ end
8
+
9
+ it "should start with a value of 1" do
10
+ @sequence.next.should == "=1"
11
+ end
12
+
13
+ describe "after being called" do
14
+ before do
15
+ @sequence.next
16
+ end
17
+
18
+ it "should use the next value" do
19
+ @sequence.next.should == "=2"
20
+ end
21
+ end
22
+ end
23
+
24
+ describe "defining a sequence" do
25
+ before do
26
+ @sequence = "sequence"
27
+ @name = :count
28
+ stub(Factory::Sequence).new { @sequence }
29
+ end
30
+
31
+ it "should create a new sequence" do
32
+ mock(Factory::Sequence).new() { @sequence }
33
+ Factory.sequence(@name)
34
+ end
35
+
36
+ it "should use the supplied block as the sequence generator" do
37
+ stub(Factory::Sequence).new.yields(1)
38
+ yielded = false
39
+ Factory.sequence(@name) {|n| yielded = true }
40
+ (yielded).should be
41
+ end
42
+ end
43
+
44
+ describe "after defining a sequence" do
45
+ before do
46
+ @sequence = "sequence"
47
+ @name = :test
48
+ @value = '1 2 5'
49
+
50
+ stub(@sequence).next { @value }
51
+ stub(Factory::Sequence).new { @sequence }
52
+
53
+ Factory.sequence(@name) {}
54
+ end
55
+
56
+ it "should call next on the sequence when sent next" do
57
+ mock(@sequence).next
58
+
59
+ Factory.next(@name)
60
+ end
61
+
62
+ it "should return the value from the sequence" do
63
+ Factory.next(@name).should == @value
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'factory_girl/syntax/blueprint'
4
+
5
+ describe "a blueprint" do
6
+ before do
7
+ Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
8
+ User.blueprint do
9
+ first_name { 'Bill' }
10
+ last_name { 'Nye' }
11
+ email { Factory.next(:email) }
12
+ end
13
+ end
14
+
15
+ after do
16
+ Factory.factories.clear
17
+ Factory.sequences.clear
18
+ end
19
+
20
+ describe "after making an instance" do
21
+ before do
22
+ @instance = Factory(:user, :last_name => 'Rye')
23
+ end
24
+
25
+ it "should use attributes from the blueprint" do
26
+ @instance.first_name.should == 'Bill'
27
+ end
28
+
29
+ it "should evaluate attribute blocks for each instance" do
30
+ @instance.email.should =~ /somebody\d+@example.com/
31
+ Factory(:user).email.should_not == @instance.email
32
+ # Factory(:user).email.should == @instance.email
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,57 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ require 'factory_girl/syntax/generate'
4
+
5
+ describe "a factory using generate syntax" do
6
+ before do
7
+ Factory.define :user do |factory|
8
+ factory.first_name 'Bill'
9
+ factory.last_name 'Nye'
10
+ factory.email 'science@guys.net'
11
+ end
12
+ end
13
+
14
+ after do
15
+ Factory.factories.clear
16
+ end
17
+
18
+ it "should not raise an error when generating an invalid instance" do
19
+ lambda { User.generate(:first_name => nil) }.should_not raise_error
20
+ end
21
+
22
+ it "should raise an error when forcefully generating an invalid instance" do
23
+ lambda { User.generate!(:first_name => nil) }.should raise_error(ActiveRecord::RecordInvalid)
24
+ end
25
+
26
+ %w(generate generate! spawn).each do |method|
27
+ it "should yield a generated instance when using #{method} with a block" do
28
+ saved_instance = nil
29
+ User.send(method) {|instance| saved_instance = instance }
30
+ saved_instance.should be_kind_of(User)
31
+ end
32
+
33
+ describe "after generating an instance using #{method}" do
34
+ before do
35
+ @instance = User.send(method, :last_name => 'Rye')
36
+ end
37
+
38
+ it "should use attributes from the factory" do
39
+ @instance.first_name.should == 'Bill'
40
+ end
41
+
42
+ it "should use attributes passed to generate" do
43
+ @instance.last_name.should == 'Rye'
44
+ end
45
+
46
+ if method == 'spawn'
47
+ it "should not save the record" do
48
+ @instance.should be_new_record
49
+ end
50
+ else
51
+ it "should save the record" do
52
+ @instance.should_not be_new_record
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'factory_girl/syntax/make'
4
+
5
+ describe "a factory using make syntax" do
6
+ before do
7
+ Factory.define :user do |factory|
8
+ factory.first_name 'Bill'
9
+ factory.last_name 'Nye'
10
+ factory.email 'science@guys.net'
11
+ end
12
+ end
13
+
14
+ after do
15
+ Factory.factories.clear
16
+ end
17
+
18
+ describe "after making an instance" do
19
+ before do
20
+ @instance = User.make(:last_name => 'Rye')
21
+ end
22
+
23
+ it "should use attributes from the factory" do
24
+ @instance.first_name.should == 'Bill'
25
+ end
26
+
27
+ it "should use attributes passed to make" do
28
+ @instance.last_name.should == 'Rye'
29
+ end
30
+
31
+ it "should save the record" do
32
+ @instance.should_not be_new_record
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ require 'factory_girl/syntax/sham'
4
+
5
+ describe "a factory using sham syntax" do
6
+ before do
7
+ Sham.name { "Name" }
8
+ Sham.email { "somebody#{rand(5)}@example.com" }
9
+
10
+ Factory.define :user do |factory|
11
+ factory.first_name { Sham.name }
12
+ factory.last_name { Sham.name }
13
+ factory.email { Sham.email }
14
+ end
15
+ end
16
+
17
+ after do
18
+ Factory.factories.clear
19
+ Factory.sequences.clear
20
+ end
21
+
22
+ describe "after making an instance" do
23
+ before do
24
+ @instance = Factory(:user, :last_name => 'Rye')
25
+ end
26
+
27
+ it "should support a sham called 'name'" do
28
+ @instance.first_name.should == 'Name'
29
+ end
30
+
31
+ it "should use the sham for the email" do
32
+ @instance.email.should =~ /somebody\d@example.com/
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,265 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
+
3
+ describe "integration" do
4
+ before do
5
+ Factory.define :user, :class => 'user' do |f|
6
+ f.first_name 'Jimi'
7
+ f.last_name 'Hendrix'
8
+ f.admin false
9
+ f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
10
+ end
11
+
12
+ Factory.define Post, :default_strategy => :attributes_for do |f|
13
+ f.name 'Test Post'
14
+ f.association :author, :factory => :user
15
+ end
16
+
17
+ Factory.define :admin, :class => User do |f|
18
+ f.first_name 'Ben'
19
+ f.last_name 'Stein'
20
+ f.admin true
21
+ f.sequence(:username) { |n| "username#{n}" }
22
+ f.email { Factory.next(:email) }
23
+ end
24
+
25
+ Factory.define :sequence_abuser, :class => User do |f|
26
+ f.first_name { Factory.sequence(:email) }
27
+ end
28
+
29
+ Factory.define :guest, :parent => :user do |f|
30
+ f.last_name 'Anonymous'
31
+ f.username 'GuestUser'
32
+ end
33
+
34
+ Factory.sequence :email do |n|
35
+ "somebody#{n}@example.com"
36
+ end
37
+ end
38
+
39
+ after do
40
+ Factory.factories.clear
41
+ end
42
+
43
+ describe "a generated attributes hash" do
44
+
45
+ before do
46
+ @attrs = Factory.attributes_for(:user, :first_name => 'Bill')
47
+ end
48
+
49
+ it "should assign all attributes" do
50
+ expected_attrs = [:admin, :email, :first_name, :last_name]
51
+ actual_attrs = @attrs.keys.sort {|a, b| a.to_s <=> b.to_s }
52
+ actual_attrs.should == expected_attrs
53
+ end
54
+
55
+ it "should correctly assign lazy, dependent attributes" do
56
+ @attrs[:email].should == "bill.hendrix@example.com"
57
+ end
58
+
59
+ it "should override attrbutes" do
60
+ @attrs[:first_name].should == 'Bill'
61
+ end
62
+
63
+ it "should not assign associations" do
64
+ Factory.attributes_for(:post)[:author].should be_nil
65
+ end
66
+
67
+ end
68
+
69
+ describe "a built instance" do
70
+
71
+ before do
72
+ @instance = Factory.build(:post)
73
+ end
74
+
75
+ it "should not be saved" do
76
+ @instance.should be_new_record
77
+ end
78
+
79
+ it "should assign associations" do
80
+ @instance.author.should be_kind_of(User)
81
+ end
82
+
83
+ it "should save associations" do
84
+ @instance.author.should_not be_new_record
85
+ end
86
+
87
+ it "should not assign both an association and its foreign key" do
88
+ Factory.build(:post, :author_id => 1).author_id.should == 1
89
+ end
90
+
91
+ end
92
+
93
+ describe "a created instance" do
94
+
95
+ before do
96
+ @instance = Factory.create('post')
97
+ end
98
+
99
+ it "should be saved" do
100
+ @instance.should_not be_new_record
101
+ end
102
+
103
+ it "should assign associations" do
104
+ @instance.author.should be_kind_of(User)
105
+ end
106
+
107
+ it "should save associations" do
108
+ @instance.author.should_not be_new_record
109
+ end
110
+
111
+ end
112
+
113
+ describe "a generated stub instance" do
114
+
115
+ before do
116
+ @stub = Factory.stub(:user, :first_name => 'Bill')
117
+ end
118
+
119
+ it "should assign all attributes" do
120
+ [:admin, :email, :first_name, :last_name].each do |attr|
121
+ @stub.send(attr).should_not be_nil
122
+ end
123
+ end
124
+
125
+ it "should correctly assign attributes" do
126
+ @stub.email.should == "bill.hendrix@example.com"
127
+ end
128
+
129
+ it "should override attributes" do
130
+ @stub.first_name.should == 'Bill'
131
+ end
132
+
133
+ it "should assign associations" do
134
+ Factory.stub(:post).author.should_not be_nil
135
+ end
136
+
137
+ it "should have an id" do
138
+ @stub.id.should > 0
139
+ end
140
+
141
+ it "should have unique IDs" do
142
+ @other_stub = Factory.stub(:user)
143
+ @stub.id.should_not == @other_stub.id
144
+ end
145
+
146
+ it "should not be considered a new record" do
147
+ @stub.should_not be_new_record
148
+ end
149
+
150
+ it "should raise exception if connection to the database is attempted" do
151
+ lambda { @stub.connection }.should raise_error(RuntimeError)
152
+ lambda { @stub.update_attribute(:first_name, "Nick") }.should raise_error(RuntimeError)
153
+ lambda { @stub.reload }.should raise_error(RuntimeError)
154
+ lambda { @stub.destroy }.should raise_error(RuntimeError)
155
+ lambda { @stub.save }.should raise_error(RuntimeError)
156
+ lambda { @stub.increment!(:age) }.should raise_error(RuntimeError)
157
+ end
158
+ end
159
+
160
+ describe "an instance generated by a factory with a custom class name" do
161
+
162
+ before do
163
+ @instance = Factory.create(:admin)
164
+ end
165
+
166
+ it "should use the correct class name" do
167
+ @instance.should be_kind_of(User)
168
+ end
169
+
170
+ it "should use the correct factory definition" do
171
+ @instance.should be_admin
172
+ end
173
+
174
+ end
175
+
176
+ describe "an instance generated by a factory that inherits from another factory" do
177
+ before do
178
+ @instance = Factory.create(:guest)
179
+ end
180
+
181
+ it "should use the class name of the parent factory" do
182
+ @instance.should be_kind_of(User)
183
+ end
184
+
185
+ it "should have attributes of the parent" do
186
+ @instance.first_name.should == 'Jimi'
187
+ end
188
+
189
+ it "should have attributes defined in the factory itself" do
190
+ @instance.username.should == 'GuestUser'
191
+ end
192
+
193
+ it "should have attributes that have been overriden" do
194
+ @instance.last_name.should == 'Anonymous'
195
+ end
196
+ end
197
+
198
+ describe "an attribute generated by a sequence" do
199
+
200
+ before do
201
+ @email = Factory.attributes_for(:admin)[:email]
202
+ end
203
+
204
+ it "should match the correct format" do
205
+ @email.should =~ /^somebody\d+@example\.com$/
206
+ end
207
+
208
+ describe "after the attribute has already been generated once" do
209
+
210
+ before do
211
+ @another_email = Factory.attributes_for(:admin)[:email]
212
+ end
213
+
214
+ it "should match the correct format" do
215
+ @email.should =~ /^somebody\d+@example\.com$/
216
+ end
217
+
218
+ it "should not be the same as the first generated value" do
219
+ @another_email.should_not == @email
220
+ end
221
+
222
+ end
223
+
224
+ end
225
+
226
+ describe "an attribute generated by an in-line sequence" do
227
+
228
+ before do
229
+ @username = Factory.attributes_for(:admin)[:username]
230
+ end
231
+
232
+ it "should match the correct format" do
233
+ @username.should =~ /^username\d+$/
234
+ end
235
+
236
+ describe "after the attribute has already been generated once" do
237
+
238
+ before do
239
+ @another_username = Factory.attributes_for(:admin)[:username]
240
+ end
241
+
242
+ it "should match the correct format" do
243
+ @username.should =~ /^username\d+$/
244
+ end
245
+
246
+ it "should not be the same as the first generated value" do
247
+ @another_username.should_not == @username
248
+ end
249
+
250
+ end
251
+
252
+ end
253
+
254
+ describe "a factory with a default strategy specified" do
255
+ it "should generate instances according to the strategy" do
256
+ Factory(:post).should be_kind_of(Hash)
257
+ end
258
+ end
259
+
260
+ it "should raise Factory::SequenceAbuseError" do
261
+ lambda {
262
+ Factory(:sequence_abuser)
263
+ }.should raise_error(Factory::SequenceAbuseError)
264
+ end
265
+ end