malvestuto_factory_girl 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +9 -0
  2. data/Changelog +29 -0
  3. data/LICENSE +19 -0
  4. data/README.rdoc +274 -0
  5. data/Rakefile +79 -0
  6. data/VERSION +1 -0
  7. data/lib/factory_girl/aliases.rb +50 -0
  8. data/lib/factory_girl/attribute/association.rb +20 -0
  9. data/lib/factory_girl/attribute/callback.rb +16 -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/attribute.rb +29 -0
  13. data/lib/factory_girl/factory.rb +432 -0
  14. data/lib/factory_girl/proxy/attributes_for.rb +21 -0
  15. data/lib/factory_girl/proxy/build.rb +30 -0
  16. data/lib/factory_girl/proxy/create.rb +12 -0
  17. data/lib/factory_girl/proxy/stub.rb +50 -0
  18. data/lib/factory_girl/proxy.rb +79 -0
  19. data/lib/factory_girl/sequence.rb +63 -0
  20. data/lib/factory_girl/step_definitions.rb +55 -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/lib/factory_girl/syntax.rb +12 -0
  26. data/lib/factory_girl.rb +35 -0
  27. data/spec/factory_girl/aliases_spec.rb +29 -0
  28. data/spec/factory_girl/attribute/association_spec.rb +29 -0
  29. data/spec/factory_girl/attribute/callback_spec.rb +23 -0
  30. data/spec/factory_girl/attribute/dynamic_spec.rb +49 -0
  31. data/spec/factory_girl/attribute/static_spec.rb +29 -0
  32. data/spec/factory_girl/attribute_spec.rb +30 -0
  33. data/spec/factory_girl/factory_spec.rb +583 -0
  34. data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
  35. data/spec/factory_girl/proxy/build_spec.rb +81 -0
  36. data/spec/factory_girl/proxy/create_spec.rb +94 -0
  37. data/spec/factory_girl/proxy/stub_spec.rb +79 -0
  38. data/spec/factory_girl/proxy_spec.rb +84 -0
  39. data/spec/factory_girl/sequence_spec.rb +66 -0
  40. data/spec/factory_girl/syntax/blueprint_spec.rb +34 -0
  41. data/spec/factory_girl/syntax/generate_spec.rb +57 -0
  42. data/spec/factory_girl/syntax/make_spec.rb +35 -0
  43. data/spec/factory_girl/syntax/sham_spec.rb +35 -0
  44. data/spec/integration_spec.rb +304 -0
  45. data/spec/models.rb +43 -0
  46. data/spec/spec_helper.rb +18 -0
  47. metadata +218 -0
@@ -0,0 +1,304 @@
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.define :user_with_callbacks, :parent => :user do |f|
35
+ f.after_stub {|u| u.first_name = 'Stubby' }
36
+ f.after_build {|u| u.first_name = 'Buildy' }
37
+ f.after_create {|u| u.last_name = 'Createy' }
38
+ end
39
+
40
+ Factory.define :user_with_inherited_callbacks, :parent => :user_with_callbacks do |f|
41
+ f.callback(:after_stub) {|u| u.last_name = 'Double-Stubby' }
42
+ end
43
+
44
+ Factory.define :business do |f|
45
+ f.name 'Supplier of Awesome'
46
+ f.association :owner, :factory => :user
47
+ end
48
+
49
+ Factory.sequence :email do |n|
50
+ "somebody#{n}@example.com"
51
+ end
52
+ end
53
+
54
+ after do
55
+ Factory.factories.clear
56
+ end
57
+
58
+ describe "a generated attributes hash" do
59
+
60
+ before do
61
+ @attrs = Factory.attributes_for(:user, :first_name => 'Bill')
62
+ end
63
+
64
+ it "should assign all attributes" do
65
+ expected_attrs = [:admin, :email, :first_name, :last_name]
66
+ actual_attrs = @attrs.keys.sort {|a, b| a.to_s <=> b.to_s }
67
+ actual_attrs.should == expected_attrs
68
+ end
69
+
70
+ it "should correctly assign lazy, dependent attributes" do
71
+ @attrs[:email].should == "bill.hendrix@example.com"
72
+ end
73
+
74
+ it "should override attrbutes" do
75
+ @attrs[:first_name].should == 'Bill'
76
+ end
77
+
78
+ it "should not assign associations" do
79
+ Factory.attributes_for(:post)[:author].should be_nil
80
+ end
81
+
82
+ end
83
+
84
+ describe "a built instance" do
85
+
86
+ before do
87
+ @instance = Factory.build(:post)
88
+ end
89
+
90
+ it "should not be saved" do
91
+ @instance.should be_new_record
92
+ end
93
+
94
+ it "should assign associations" do
95
+ @instance.author.should be_kind_of(User)
96
+ end
97
+
98
+ it "should save associations" do
99
+ @instance.author.should_not be_new_record
100
+ end
101
+
102
+ it "should not assign both an association and its foreign key" do
103
+ Factory.build(:post, :author_id => 1).author_id.should == 1
104
+ end
105
+
106
+ end
107
+
108
+ describe "a created instance" do
109
+
110
+ before do
111
+ @instance = Factory.create('post')
112
+ end
113
+
114
+ it "should be saved" do
115
+ @instance.should_not be_new_record
116
+ end
117
+
118
+ it "should assign associations" do
119
+ @instance.author.should be_kind_of(User)
120
+ end
121
+
122
+ it "should save associations" do
123
+ @instance.author.should_not be_new_record
124
+ end
125
+
126
+ end
127
+
128
+ describe "a generated stub instance" do
129
+
130
+ before do
131
+ @stub = Factory.stub(:user, :first_name => 'Bill')
132
+ end
133
+
134
+ it "should assign all attributes" do
135
+ [:admin, :email, :first_name, :last_name].each do |attr|
136
+ @stub.send(attr).should_not be_nil
137
+ end
138
+ end
139
+
140
+ it "should correctly assign attributes" do
141
+ @stub.email.should == "bill.hendrix@example.com"
142
+ end
143
+
144
+ it "should override attributes" do
145
+ @stub.first_name.should == 'Bill'
146
+ end
147
+
148
+ it "should assign associations" do
149
+ Factory.stub(:post).author.should_not be_nil
150
+ end
151
+
152
+ it "should have an id" do
153
+ @stub.id.should > 0
154
+ end
155
+
156
+ it "should have unique IDs" do
157
+ @other_stub = Factory.stub(:user)
158
+ @stub.id.should_not == @other_stub.id
159
+ end
160
+
161
+ it "should not be considered a new record" do
162
+ @stub.should_not be_new_record
163
+ end
164
+
165
+ it "should raise exception if connection to the database is attempted" do
166
+ lambda { @stub.connection }.should raise_error(RuntimeError)
167
+ lambda { @stub.update_attribute(:first_name, "Nick") }.should raise_error(RuntimeError)
168
+ lambda { @stub.reload }.should raise_error(RuntimeError)
169
+ lambda { @stub.destroy }.should raise_error(RuntimeError)
170
+ lambda { @stub.save }.should raise_error(RuntimeError)
171
+ lambda { @stub.increment!(:age) }.should raise_error(RuntimeError)
172
+ end
173
+ end
174
+
175
+ describe "an instance generated by a factory with a custom class name" do
176
+
177
+ before do
178
+ @instance = Factory.create(:admin)
179
+ end
180
+
181
+ it "should use the correct class name" do
182
+ @instance.should be_kind_of(User)
183
+ end
184
+
185
+ it "should use the correct factory definition" do
186
+ @instance.should be_admin
187
+ end
188
+
189
+ end
190
+
191
+ describe "an instance generated by a factory that inherits from another factory" do
192
+ before do
193
+ @instance = Factory.create(:guest)
194
+ end
195
+
196
+ it "should use the class name of the parent factory" do
197
+ @instance.should be_kind_of(User)
198
+ end
199
+
200
+ it "should have attributes of the parent" do
201
+ @instance.first_name.should == 'Jimi'
202
+ end
203
+
204
+ it "should have attributes defined in the factory itself" do
205
+ @instance.username.should == 'GuestUser'
206
+ end
207
+
208
+ it "should have attributes that have been overriden" do
209
+ @instance.last_name.should == 'Anonymous'
210
+ end
211
+ end
212
+
213
+ describe "an attribute generated by a sequence" do
214
+
215
+ before do
216
+ @email = Factory.attributes_for(:admin)[:email]
217
+ end
218
+
219
+ it "should match the correct format" do
220
+ @email.should =~ /^somebody\d+@example\.com$/
221
+ end
222
+
223
+ describe "after the attribute has already been generated once" do
224
+
225
+ before do
226
+ @another_email = Factory.attributes_for(:admin)[:email]
227
+ end
228
+
229
+ it "should match the correct format" do
230
+ @email.should =~ /^somebody\d+@example\.com$/
231
+ end
232
+
233
+ it "should not be the same as the first generated value" do
234
+ @another_email.should_not == @email
235
+ end
236
+
237
+ end
238
+
239
+ end
240
+
241
+ describe "an attribute generated by an in-line sequence" do
242
+
243
+ before do
244
+ @username = Factory.attributes_for(:admin)[:username]
245
+ end
246
+
247
+ it "should match the correct format" do
248
+ @username.should =~ /^username\d+$/
249
+ end
250
+
251
+ describe "after the attribute has already been generated once" do
252
+
253
+ before do
254
+ @another_username = Factory.attributes_for(:admin)[:username]
255
+ end
256
+
257
+ it "should match the correct format" do
258
+ @username.should =~ /^username\d+$/
259
+ end
260
+
261
+ it "should not be the same as the first generated value" do
262
+ @another_username.should_not == @username
263
+ end
264
+
265
+ end
266
+
267
+ end
268
+
269
+ describe "a factory with a default strategy specified" do
270
+ it "should generate instances according to the strategy" do
271
+ Factory(:post).should be_kind_of(Hash)
272
+ end
273
+ end
274
+
275
+ it "should raise Factory::SequenceAbuseError" do
276
+ lambda {
277
+ Factory(:sequence_abuser)
278
+ }.should raise_error(Factory::SequenceAbuseError)
279
+ end
280
+
281
+ describe "an instance with callbacks" do
282
+ it "should run the after_stub callback when stubbing" do
283
+ @user = Factory.stub(:user_with_callbacks)
284
+ @user.first_name.should == 'Stubby'
285
+ end
286
+
287
+ it "should run the after_build callback when building" do
288
+ @user = Factory.build(:user_with_callbacks)
289
+ @user.first_name.should == 'Buildy'
290
+ end
291
+
292
+ it "should run both the after_build and after_create callbacks when creating" do
293
+ @user = Factory(:user_with_callbacks)
294
+ @user.first_name.should == 'Buildy'
295
+ @user.last_name.should == 'Createy'
296
+ end
297
+
298
+ it "should run both the after_stub callback on the factory and the inherited after_stub callback" do
299
+ @user = Factory.stub(:user_with_inherited_callbacks)
300
+ @user.first_name.should == 'Stubby'
301
+ @user.last_name.should == 'Double-Stubby'
302
+ end
303
+ end
304
+ 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
metadata ADDED
@@ -0,0 +1,218 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: malvestuto_factory_girl
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 2
9
+ - 5
10
+ version: 1.2.5
11
+ platform: ruby
12
+ authors:
13
+ - Joe Ferris
14
+ - Bruno Malvestuto
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-03-30 00:00:00 -03:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: rcov
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ type: :development
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: cucumber
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ type: :development
63
+ version_requirements: *id003
64
+ - !ruby/object:Gem::Dependency
65
+ name: activerecord
66
+ prerelease: false
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ type: :development
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ name: rr
80
+ prerelease: false
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ type: :development
91
+ version_requirements: *id005
92
+ - !ruby/object:Gem::Dependency
93
+ name: sqlite3
94
+ prerelease: false
95
+ requirement: &id006 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ type: :development
105
+ version_requirements: *id006
106
+ description: |-
107
+ factory_girl provides a framework and DSL for defining and
108
+ using factories - less error-prone, more explicit, and
109
+ all-around easier to work with than fixtures.
110
+ email: jferris@thoughtbot.com
111
+ executables: []
112
+
113
+ extensions: []
114
+
115
+ extra_rdoc_files:
116
+ - README.rdoc
117
+ files:
118
+ - CONTRIBUTION_GUIDELINES.rdoc
119
+ - Changelog
120
+ - LICENSE
121
+ - README.rdoc
122
+ - Rakefile
123
+ - VERSION
124
+ - lib/factory_girl.rb
125
+ - lib/factory_girl/aliases.rb
126
+ - lib/factory_girl/attribute.rb
127
+ - lib/factory_girl/attribute/association.rb
128
+ - lib/factory_girl/attribute/callback.rb
129
+ - lib/factory_girl/attribute/dynamic.rb
130
+ - lib/factory_girl/attribute/static.rb
131
+ - lib/factory_girl/factory.rb
132
+ - lib/factory_girl/proxy.rb
133
+ - lib/factory_girl/proxy/attributes_for.rb
134
+ - lib/factory_girl/proxy/build.rb
135
+ - lib/factory_girl/proxy/create.rb
136
+ - lib/factory_girl/proxy/stub.rb
137
+ - lib/factory_girl/sequence.rb
138
+ - lib/factory_girl/step_definitions.rb
139
+ - lib/factory_girl/syntax.rb
140
+ - lib/factory_girl/syntax/blueprint.rb
141
+ - lib/factory_girl/syntax/generate.rb
142
+ - lib/factory_girl/syntax/make.rb
143
+ - lib/factory_girl/syntax/sham.rb
144
+ - spec/factory_girl/aliases_spec.rb
145
+ - spec/factory_girl/attribute/association_spec.rb
146
+ - spec/factory_girl/attribute/callback_spec.rb
147
+ - spec/factory_girl/attribute/dynamic_spec.rb
148
+ - spec/factory_girl/attribute/static_spec.rb
149
+ - spec/factory_girl/attribute_spec.rb
150
+ - spec/factory_girl/factory_spec.rb
151
+ - spec/factory_girl/proxy/attributes_for_spec.rb
152
+ - spec/factory_girl/proxy/build_spec.rb
153
+ - spec/factory_girl/proxy/create_spec.rb
154
+ - spec/factory_girl/proxy/stub_spec.rb
155
+ - spec/factory_girl/proxy_spec.rb
156
+ - spec/factory_girl/sequence_spec.rb
157
+ - spec/factory_girl/syntax/blueprint_spec.rb
158
+ - spec/factory_girl/syntax/generate_spec.rb
159
+ - spec/factory_girl/syntax/make_spec.rb
160
+ - spec/factory_girl/syntax/sham_spec.rb
161
+ - spec/integration_spec.rb
162
+ - spec/models.rb
163
+ - spec/spec_helper.rb
164
+ has_rdoc: true
165
+ homepage: http://thoughtbot.com/projects/factory_girl
166
+ licenses: []
167
+
168
+ post_install_message:
169
+ rdoc_options:
170
+ - --line-numbers
171
+ - --main
172
+ - README.rdoc
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ none: false
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ hash: 3
181
+ segments:
182
+ - 0
183
+ version: "0"
184
+ required_rubygems_version: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ hash: 3
190
+ segments:
191
+ - 0
192
+ version: "0"
193
+ requirements: []
194
+
195
+ rubyforge_project:
196
+ rubygems_version: 1.3.7
197
+ signing_key:
198
+ specification_version: 3
199
+ summary: factory_girl provides a framework and DSL for defining and using model instance factories.
200
+ test_files:
201
+ - spec/factory_girl/aliases_spec.rb
202
+ - spec/factory_girl/attribute/association_spec.rb
203
+ - spec/factory_girl/attribute/callback_spec.rb
204
+ - spec/factory_girl/attribute/dynamic_spec.rb
205
+ - spec/factory_girl/attribute/static_spec.rb
206
+ - spec/factory_girl/attribute_spec.rb
207
+ - spec/factory_girl/factory_spec.rb
208
+ - spec/factory_girl/proxy/attributes_for_spec.rb
209
+ - spec/factory_girl/proxy/build_spec.rb
210
+ - spec/factory_girl/proxy/create_spec.rb
211
+ - spec/factory_girl/proxy/stub_spec.rb
212
+ - spec/factory_girl/proxy_spec.rb
213
+ - spec/factory_girl/sequence_spec.rb
214
+ - spec/factory_girl/syntax/blueprint_spec.rb
215
+ - spec/factory_girl/syntax/generate_spec.rb
216
+ - spec/factory_girl/syntax/make_spec.rb
217
+ - spec/factory_girl/syntax/sham_spec.rb
218
+ - spec/integration_spec.rb