agibralter-factory_girl 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +10 -0
  2. data/Changelog +29 -0
  3. data/LICENSE +19 -0
  4. data/README.rdoc +228 -0
  5. data/Rakefile +74 -0
  6. data/lib/factory_girl.rb +35 -0
  7. data/lib/factory_girl/aliases.rb +50 -0
  8. data/lib/factory_girl/attribute.rb +29 -0
  9. data/lib/factory_girl/attribute/association.rb +18 -0
  10. data/lib/factory_girl/attribute/dynamic.rb +20 -0
  11. data/lib/factory_girl/attribute/static.rb +17 -0
  12. data/lib/factory_girl/callback.rb +25 -0
  13. data/lib/factory_girl/factory.rb +393 -0
  14. data/lib/factory_girl/proxy.rb +66 -0
  15. data/lib/factory_girl/proxy/attributes_for.rb +21 -0
  16. data/lib/factory_girl/proxy/build.rb +29 -0
  17. data/lib/factory_girl/proxy/create.rb +24 -0
  18. data/lib/factory_girl/proxy/stub.rb +49 -0
  19. data/lib/factory_girl/sequence.rb +63 -0
  20. data/lib/factory_girl/syntax.rb +12 -0
  21. data/lib/factory_girl/syntax/blueprint.rb +42 -0
  22. data/lib/factory_girl/syntax/generate.rb +68 -0
  23. data/lib/factory_girl/syntax/make.rb +39 -0
  24. data/lib/factory_girl/syntax/sham.rb +42 -0
  25. data/spec/factory_girl/aliases_spec.rb +29 -0
  26. data/spec/factory_girl/attribute/association_spec.rb +25 -0
  27. data/spec/factory_girl/attribute/dynamic_spec.rb +49 -0
  28. data/spec/factory_girl/attribute/static_spec.rb +29 -0
  29. data/spec/factory_girl/attribute_spec.rb +30 -0
  30. data/spec/factory_girl/callback_spec.rb +43 -0
  31. data/spec/factory_girl/factory_spec.rb +519 -0
  32. data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
  33. data/spec/factory_girl/proxy/build_spec.rb +73 -0
  34. data/spec/factory_girl/proxy/create_spec.rb +83 -0
  35. data/spec/factory_girl/proxy/stub_spec.rb +69 -0
  36. data/spec/factory_girl/proxy_spec.rb +28 -0
  37. data/spec/factory_girl/sequence_spec.rb +66 -0
  38. data/spec/factory_girl/syntax/blueprint_spec.rb +34 -0
  39. data/spec/factory_girl/syntax/generate_spec.rb +57 -0
  40. data/spec/factory_girl/syntax/make_spec.rb +35 -0
  41. data/spec/factory_girl/syntax/sham_spec.rb +35 -0
  42. data/spec/integration_spec.rb +314 -0
  43. data/spec/models.rb +43 -0
  44. data/spec/spec_helper.rb +18 -0
  45. metadata +116 -0
@@ -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,314 @@
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 :user_with_before_save, :class => User do |f|
13
+ f.first_name 'Nat'
14
+ f.last_name 'Portman'
15
+ f.admin false
16
+ f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
17
+ f.add_callback(:before_save) do |u|
18
+ u.first_name = 'Natalie'
19
+ end
20
+ end
21
+
22
+ Factory.define :user_with_after_save, :class => User do |f|
23
+ f.first_name 'Nat'
24
+ f.last_name 'Portman'
25
+ f.admin false
26
+ f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
27
+ f.add_callback(:after_save) do |u|
28
+ u.last_name = 'Alba'
29
+ u.save!
30
+ u.first_name = 'Jessica'
31
+ end
32
+ end
33
+
34
+ Factory.define Post, :default_strategy => :attributes_for do |f|
35
+ f.name 'Test Post'
36
+ f.association :author, :factory => :user
37
+ end
38
+
39
+ Factory.define :admin, :class => User do |f|
40
+ f.first_name 'Ben'
41
+ f.last_name 'Stein'
42
+ f.admin true
43
+ f.sequence(:username) { |n| "username#{n}" }
44
+ f.email { Factory.next(:email) }
45
+ end
46
+
47
+ Factory.define :sequence_abuser, :class => User do |f|
48
+ f.first_name { Factory.sequence(:email) }
49
+ end
50
+
51
+ Factory.define :guest, :parent => :user do |f|
52
+ f.last_name 'Anonymous'
53
+ f.username 'GuestUser'
54
+ end
55
+
56
+ Factory.sequence :email do |n|
57
+ "somebody#{n}@example.com"
58
+ end
59
+ end
60
+
61
+ after do
62
+ Factory.factories.clear
63
+ end
64
+
65
+ describe "a generated attributes hash" do
66
+
67
+ before do
68
+ @attrs = Factory.attributes_for(:user, :first_name => 'Bill')
69
+ end
70
+
71
+ it "should assign all attributes" do
72
+ expected_attrs = [:admin, :email, :first_name, :last_name]
73
+ actual_attrs = @attrs.keys.sort {|a, b| a.to_s <=> b.to_s }
74
+ actual_attrs.should == expected_attrs
75
+ end
76
+
77
+ it "should correctly assign lazy, dependent attributes" do
78
+ @attrs[:email].should == "bill.hendrix@example.com"
79
+ end
80
+
81
+ it "should override attrbutes" do
82
+ @attrs[:first_name].should == 'Bill'
83
+ end
84
+
85
+ it "should not assign associations" do
86
+ Factory.attributes_for(:post)[:author].should be_nil
87
+ end
88
+
89
+ end
90
+
91
+ describe "a built instance" do
92
+
93
+ before do
94
+ @instance = Factory.build(:post)
95
+ end
96
+
97
+ it "should not be saved" do
98
+ @instance.should be_new_record
99
+ end
100
+
101
+ it "should assign associations" do
102
+ @instance.author.should be_kind_of(User)
103
+ end
104
+
105
+ it "should save associations" do
106
+ @instance.author.should_not be_new_record
107
+ end
108
+
109
+ it "should not assign both an association and its foreign key" do
110
+ Factory.build(:post, :author_id => 1).author_id.should == 1
111
+ end
112
+
113
+ end
114
+
115
+ describe "a created instance" do
116
+
117
+ before do
118
+ @instance = Factory.create('post')
119
+ end
120
+
121
+ it "should be saved" do
122
+ @instance.should_not be_new_record
123
+ end
124
+
125
+ it "should assign associations" do
126
+ @instance.author.should be_kind_of(User)
127
+ end
128
+
129
+ it "should save associations" do
130
+ @instance.author.should_not be_new_record
131
+ end
132
+
133
+ end
134
+
135
+ describe "a created instance with before_save callbacks" do
136
+
137
+ before do
138
+ @instance = Factory(:user_with_before_save)
139
+ end
140
+
141
+ it "should call before_save callbacks" do
142
+ @instance.first_name.should == 'Natalie'
143
+ end
144
+
145
+ end
146
+
147
+ describe "a created instance with after_save callbacks" do
148
+
149
+ before do
150
+ @instance = Factory(:user_with_after_save)
151
+ end
152
+
153
+ it "should call after_save callbacks" do
154
+ @instance.last_name.should == 'Alba'
155
+ end
156
+
157
+ it "should obey model state" do
158
+ User.find_by_last_name('Alba').first_name.should == 'Nat' # not Jessica
159
+ end
160
+ end
161
+
162
+ describe "a generated stub instance" do
163
+
164
+ before do
165
+ @stub = Factory.stub(:user, :first_name => 'Bill')
166
+ end
167
+
168
+ it "should assign all attributes" do
169
+ [:admin, :email, :first_name, :last_name].each do |attr|
170
+ @stub.send(attr).should_not be_nil
171
+ end
172
+ end
173
+
174
+ it "should correctly assign attributes" do
175
+ @stub.email.should == "bill.hendrix@example.com"
176
+ end
177
+
178
+ it "should override attributes" do
179
+ @stub.first_name.should == 'Bill'
180
+ end
181
+
182
+ it "should assign associations" do
183
+ Factory.stub(:post).author.should_not be_nil
184
+ end
185
+
186
+ it "should have an id" do
187
+ @stub.id.should > 0
188
+ end
189
+
190
+ it "should have unique IDs" do
191
+ @other_stub = Factory.stub(:user)
192
+ @stub.id.should_not == @other_stub.id
193
+ end
194
+
195
+ it "should not be considered a new record" do
196
+ @stub.should_not be_new_record
197
+ end
198
+
199
+ it "should raise exception if connection to the database is attempted" do
200
+ lambda { @stub.connection }.should raise_error(RuntimeError)
201
+ lambda { @stub.update_attribute(:first_name, "Nick") }.should raise_error(RuntimeError)
202
+ lambda { @stub.reload }.should raise_error(RuntimeError)
203
+ lambda { @stub.destroy }.should raise_error(RuntimeError)
204
+ lambda { @stub.save }.should raise_error(RuntimeError)
205
+ lambda { @stub.increment!(:age) }.should raise_error(RuntimeError)
206
+ end
207
+ end
208
+
209
+ describe "an instance generated by a factory with a custom class name" do
210
+
211
+ before do
212
+ @instance = Factory.create(:admin)
213
+ end
214
+
215
+ it "should use the correct class name" do
216
+ @instance.should be_kind_of(User)
217
+ end
218
+
219
+ it "should use the correct factory definition" do
220
+ @instance.should be_admin
221
+ end
222
+
223
+ end
224
+
225
+ describe "an instance generated by a factory that inherits from another factory" do
226
+ before do
227
+ @instance = Factory.create(:guest)
228
+ end
229
+
230
+ it "should use the class name of the parent factory" do
231
+ @instance.should be_kind_of(User)
232
+ end
233
+
234
+ it "should have attributes of the parent" do
235
+ @instance.first_name.should == 'Jimi'
236
+ end
237
+
238
+ it "should have attributes defined in the factory itself" do
239
+ @instance.username.should == 'GuestUser'
240
+ end
241
+
242
+ it "should have attributes that have been overriden" do
243
+ @instance.last_name.should == 'Anonymous'
244
+ end
245
+ end
246
+
247
+ describe "an attribute generated by a sequence" do
248
+
249
+ before do
250
+ @email = Factory.attributes_for(:admin)[:email]
251
+ end
252
+
253
+ it "should match the correct format" do
254
+ @email.should =~ /^somebody\d+@example\.com$/
255
+ end
256
+
257
+ describe "after the attribute has already been generated once" do
258
+
259
+ before do
260
+ @another_email = Factory.attributes_for(:admin)[:email]
261
+ end
262
+
263
+ it "should match the correct format" do
264
+ @email.should =~ /^somebody\d+@example\.com$/
265
+ end
266
+
267
+ it "should not be the same as the first generated value" do
268
+ @another_email.should_not == @email
269
+ end
270
+
271
+ end
272
+
273
+ end
274
+
275
+ describe "an attribute generated by an in-line sequence" do
276
+
277
+ before do
278
+ @username = Factory.attributes_for(:admin)[:username]
279
+ end
280
+
281
+ it "should match the correct format" do
282
+ @username.should =~ /^username\d+$/
283
+ end
284
+
285
+ describe "after the attribute has already been generated once" do
286
+
287
+ before do
288
+ @another_username = Factory.attributes_for(:admin)[:username]
289
+ end
290
+
291
+ it "should match the correct format" do
292
+ @username.should =~ /^username\d+$/
293
+ end
294
+
295
+ it "should not be the same as the first generated value" do
296
+ @another_username.should_not == @username
297
+ end
298
+
299
+ end
300
+
301
+ end
302
+
303
+ describe "a factory with a default strategy specified" do
304
+ it "should generate instances according to the strategy" do
305
+ Factory(:post).should be_kind_of(Hash)
306
+ end
307
+ end
308
+
309
+ it "should raise Factory::SequenceAbuseError" do
310
+ lambda {
311
+ Factory(:sequence_abuser)
312
+ }.should raise_error(Factory::SequenceAbuseError)
313
+ end
314
+ end
data/spec/models.rb ADDED
@@ -0,0 +1,43 @@
1
+ ActiveRecord::Base.establish_connection(
2
+ :adapter => 'sqlite3',
3
+ :database => File.join(File.dirname(__FILE__), 'test.db')
4
+ )
5
+
6
+ class CreateSchema < ActiveRecord::Migration
7
+ def self.up
8
+ create_table :users, :force => true do |t|
9
+ t.string :first_name
10
+ t.string :last_name
11
+ t.string :email
12
+ t.string :username
13
+ t.boolean :admin, :default => false
14
+ end
15
+
16
+ create_table :posts, :force => true do |t|
17
+ t.string :name
18
+ t.integer :author_id
19
+ end
20
+
21
+ create_table :business, :force => true do |t|
22
+ t.string :name
23
+ t.integer :owner_id
24
+ end
25
+ end
26
+ end
27
+
28
+ CreateSchema.suppress_messages { CreateSchema.migrate(:up) }
29
+
30
+ class User < ActiveRecord::Base
31
+ validates_presence_of :first_name, :last_name, :email
32
+ has_many :posts, :foreign_key => 'author_id'
33
+ end
34
+
35
+ class Business < ActiveRecord::Base
36
+ validates_presence_of :name, :owner_id
37
+ belongs_to :owner, :class_name => 'User'
38
+ end
39
+
40
+ class Post < ActiveRecord::Base
41
+ validates_presence_of :name, :author_id
42
+ belongs_to :author, :class_name => 'User'
43
+ end
@@ -0,0 +1,18 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $: << File.join(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+
6
+ require 'activerecord'
7
+
8
+ require 'spec'
9
+ require 'spec/autorun'
10
+ require 'rr'
11
+
12
+ require 'models'
13
+
14
+ require 'factory_girl'
15
+
16
+ Spec::Runner.configure do |config|
17
+ config.mock_with RR::Adapters::Rspec
18
+ end