factory_girl 2.0.0.beta1 → 2.0.0.beta2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Appraisals +12 -0
- data/{CONTRIBUTION_GUIDELINES.rdoc → CONTRIBUTION_GUIDELINES.md} +3 -3
- data/GETTING_STARTED.md +246 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +62 -0
- data/README.md +64 -0
- data/Rakefile +15 -30
- data/features/factory_girl_steps.feature +26 -0
- data/features/step_definitions/database_steps.rb +2 -0
- data/features/support/env.rb +0 -9
- data/features/support/factories.rb +15 -0
- data/features/support/test.db +0 -0
- data/lib/factory_girl.rb +2 -0
- data/lib/factory_girl/definition_proxy.rb +10 -43
- data/lib/factory_girl/factory.rb +48 -17
- data/lib/factory_girl/proxy.rb +3 -3
- data/lib/factory_girl/proxy/attributes_for.rb +1 -1
- data/lib/factory_girl/proxy/build.rb +3 -3
- data/lib/factory_girl/proxy/create.rb +6 -2
- data/lib/factory_girl/proxy/stub.rb +3 -3
- data/lib/factory_girl/registry.rb +59 -0
- data/lib/factory_girl/sequence.rb +23 -9
- data/lib/factory_girl/step_definitions.rb +16 -9
- data/lib/factory_girl/syntax/blueprint.rb +2 -2
- data/lib/factory_girl/syntax/default.rb +5 -3
- data/lib/factory_girl/syntax/generate.rb +6 -6
- data/lib/factory_girl/syntax/make.rb +2 -2
- data/lib/factory_girl/syntax/methods.rb +75 -0
- data/lib/factory_girl/syntax/sham.rb +2 -2
- data/lib/factory_girl/syntax/vintage.rb +16 -79
- data/lib/factory_girl/version.rb +1 -1
- data/spec/acceptance/acceptance_helper.rb +7 -12
- data/spec/acceptance/attribute_aliases_spec.rb +26 -0
- data/spec/acceptance/attributes_for_spec.rb +48 -0
- data/spec/acceptance/build_spec.rb +35 -0
- data/spec/acceptance/build_stubbed_spec.rb +79 -0
- data/spec/acceptance/callbacks_spec.rb +42 -0
- data/spec/acceptance/create_spec.rb +67 -0
- data/spec/acceptance/default_strategy_spec.rb +27 -0
- data/spec/acceptance/definition_spec.rb +28 -0
- data/spec/acceptance/parent_spec.rb +39 -0
- data/spec/acceptance/sequence_spec.rb +32 -0
- data/spec/acceptance/syntax/blueprint_spec.rb +7 -5
- data/spec/acceptance/syntax/generate_spec.rb +10 -4
- data/spec/acceptance/syntax/make_spec.rb +7 -4
- data/spec/acceptance/syntax/sham_spec.rb +15 -8
- data/spec/acceptance/syntax/vintage_spec.rb +57 -17
- data/spec/factory_girl/attribute/dynamic_spec.rb +1 -1
- data/spec/factory_girl/definition_proxy_spec.rb +13 -11
- data/spec/factory_girl/factory_spec.rb +29 -52
- data/spec/factory_girl/find_definitions_spec.rb +34 -23
- data/spec/factory_girl/proxy/attributes_for_spec.rb +6 -6
- data/spec/factory_girl/proxy/build_spec.rb +4 -4
- data/spec/factory_girl/proxy/create_spec.rb +11 -3
- data/spec/factory_girl/proxy/stub_spec.rb +6 -6
- data/spec/factory_girl/proxy_spec.rb +1 -1
- data/spec/factory_girl/registry_spec.rb +92 -0
- data/spec/factory_girl/sequence_spec.rb +65 -8
- data/spec/spec_helper.rb +75 -29
- metadata +66 -43
- data/README.rdoc +0 -282
- data/spec/acceptance/acceptance_spec.rb +0 -288
- data/spec/acceptance/models.rb +0 -48
@@ -1,288 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'acceptance/acceptance_helper'
|
3
|
-
|
4
|
-
describe "integration" do
|
5
|
-
before do
|
6
|
-
FactoryGirl.define do
|
7
|
-
sequence :email do |n|
|
8
|
-
"somebody#{n}@example.com"
|
9
|
-
end
|
10
|
-
|
11
|
-
factory :user, :class => 'user' do
|
12
|
-
first_name 'Jimi'
|
13
|
-
last_name 'Hendrix'
|
14
|
-
admin false
|
15
|
-
email { "#{first_name}.#{last_name}@example.com".downcase }
|
16
|
-
|
17
|
-
aliased_as :author
|
18
|
-
end
|
19
|
-
|
20
|
-
factory Post, :default_strategy => :attributes_for do
|
21
|
-
name 'Test Post'
|
22
|
-
author
|
23
|
-
end
|
24
|
-
|
25
|
-
factory :admin, :class => User do
|
26
|
-
first_name 'Ben'
|
27
|
-
last_name 'Stein'
|
28
|
-
admin true
|
29
|
-
sequence(:username) { |n| "username#{n}" }
|
30
|
-
email
|
31
|
-
end
|
32
|
-
|
33
|
-
factory :sequence_abuser, :class => User do
|
34
|
-
first_name { Factory.sequence(:email) }
|
35
|
-
end
|
36
|
-
|
37
|
-
factory :guest, :parent => :user do
|
38
|
-
last_name 'Anonymous'
|
39
|
-
username 'GuestUser'
|
40
|
-
end
|
41
|
-
|
42
|
-
factory :user_with_callbacks, :parent => :user do
|
43
|
-
after_stub {|u| u.first_name = 'Stubby' }
|
44
|
-
after_build {|u| u.first_name = 'Buildy' }
|
45
|
-
after_create {|u| u.last_name = 'Createy' }
|
46
|
-
end
|
47
|
-
|
48
|
-
factory :user_with_inherited_callbacks, :parent => :user_with_callbacks do
|
49
|
-
after_stub {|u| u.last_name = 'Double-Stubby' }
|
50
|
-
end
|
51
|
-
|
52
|
-
factory :business do
|
53
|
-
name 'Supplier of Awesome'
|
54
|
-
association :owner, :factory => :user
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
describe "a generated attributes hash" do
|
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
|
-
end
|
82
|
-
|
83
|
-
describe "a built instance" do
|
84
|
-
before do
|
85
|
-
@instance = Factory.build(:post)
|
86
|
-
end
|
87
|
-
|
88
|
-
it "should not be saved" do
|
89
|
-
@instance.should be_new_record
|
90
|
-
end
|
91
|
-
|
92
|
-
it "should assign associations" do
|
93
|
-
@instance.author.should be_kind_of(User)
|
94
|
-
end
|
95
|
-
|
96
|
-
it "should save associations" do
|
97
|
-
@instance.author.should_not be_new_record
|
98
|
-
end
|
99
|
-
|
100
|
-
it "should not assign both an association and its foreign key" do
|
101
|
-
Factory.build(:post, :author_id => 1).author_id.should == 1
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
describe "a created instance" do
|
106
|
-
before do
|
107
|
-
@instance = Factory.create('post')
|
108
|
-
end
|
109
|
-
|
110
|
-
it "should be saved" do
|
111
|
-
@instance.should_not be_new_record
|
112
|
-
end
|
113
|
-
|
114
|
-
it "should assign associations" do
|
115
|
-
@instance.author.should be_kind_of(User)
|
116
|
-
end
|
117
|
-
|
118
|
-
it "should save associations" do
|
119
|
-
@instance.author.should_not be_new_record
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
describe "a generated stub instance" do
|
124
|
-
before do
|
125
|
-
@stub = Factory.stub(:user, :first_name => 'Bill')
|
126
|
-
end
|
127
|
-
|
128
|
-
it "should assign all attributes" do
|
129
|
-
[:admin, :email, :first_name, :last_name].each do |attr|
|
130
|
-
@stub.send(attr).should_not be_nil
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
it "should correctly assign attributes" do
|
135
|
-
@stub.email.should == "bill.hendrix@example.com"
|
136
|
-
end
|
137
|
-
|
138
|
-
it "should override attributes" do
|
139
|
-
@stub.first_name.should == 'Bill'
|
140
|
-
end
|
141
|
-
|
142
|
-
it "should assign associations" do
|
143
|
-
Factory.stub(:post).author.should_not be_nil
|
144
|
-
end
|
145
|
-
|
146
|
-
it "should have an id" do
|
147
|
-
@stub.id.should > 0
|
148
|
-
end
|
149
|
-
|
150
|
-
it "should have unique IDs" do
|
151
|
-
@other_stub = Factory.stub(:user)
|
152
|
-
@stub.id.should_not == @other_stub.id
|
153
|
-
end
|
154
|
-
|
155
|
-
it "should not be considered a new record" do
|
156
|
-
@stub.should_not be_new_record
|
157
|
-
end
|
158
|
-
|
159
|
-
it "should raise exception if connection to the database is attempted" do
|
160
|
-
lambda { @stub.connection }.should raise_error(RuntimeError)
|
161
|
-
lambda { @stub.update_attribute(:first_name, "Nick") }.should raise_error(RuntimeError)
|
162
|
-
lambda { @stub.reload }.should raise_error(RuntimeError)
|
163
|
-
lambda { @stub.destroy }.should raise_error(RuntimeError)
|
164
|
-
lambda { @stub.save }.should raise_error(RuntimeError)
|
165
|
-
lambda { @stub.increment!(:age) }.should raise_error(RuntimeError)
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
describe "an instance generated by a factory with a custom class name" do
|
170
|
-
before do
|
171
|
-
@instance = Factory.create(:admin)
|
172
|
-
end
|
173
|
-
|
174
|
-
it "should use the correct class name" do
|
175
|
-
@instance.should be_kind_of(User)
|
176
|
-
end
|
177
|
-
|
178
|
-
it "should use the correct factory definition" do
|
179
|
-
@instance.should be_admin
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
describe "an instance generated by a factory that inherits from another factory" do
|
184
|
-
before do
|
185
|
-
@instance = Factory.create(:guest)
|
186
|
-
end
|
187
|
-
|
188
|
-
it "should use the class name of the parent factory" do
|
189
|
-
@instance.should be_kind_of(User)
|
190
|
-
end
|
191
|
-
|
192
|
-
it "should have attributes of the parent" do
|
193
|
-
@instance.first_name.should == 'Jimi'
|
194
|
-
end
|
195
|
-
|
196
|
-
it "should have attributes defined in the factory itself" do
|
197
|
-
@instance.username.should == 'GuestUser'
|
198
|
-
end
|
199
|
-
|
200
|
-
it "should have attributes that have been overriden" do
|
201
|
-
@instance.last_name.should == 'Anonymous'
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
describe "an attribute generated by a sequence" do
|
206
|
-
before do
|
207
|
-
@email = Factory.attributes_for(:admin)[:email]
|
208
|
-
end
|
209
|
-
|
210
|
-
it "should match the correct format" do
|
211
|
-
@email.should =~ /^somebody\d+@example\.com$/
|
212
|
-
end
|
213
|
-
|
214
|
-
describe "after the attribute has already been generated once" do
|
215
|
-
before do
|
216
|
-
@another_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
|
-
it "should not be the same as the first generated value" do
|
224
|
-
@another_email.should_not == @email
|
225
|
-
end
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
describe "an attribute generated by an in-line sequence" do
|
230
|
-
before do
|
231
|
-
@username = Factory.attributes_for(:admin)[:username]
|
232
|
-
end
|
233
|
-
|
234
|
-
it "should match the correct format" do
|
235
|
-
@username.should =~ /^username\d+$/
|
236
|
-
end
|
237
|
-
|
238
|
-
describe "after the attribute has already been generated once" do
|
239
|
-
before do
|
240
|
-
@another_username = Factory.attributes_for(:admin)[:username]
|
241
|
-
end
|
242
|
-
|
243
|
-
it "should match the correct format" do
|
244
|
-
@username.should =~ /^username\d+$/
|
245
|
-
end
|
246
|
-
|
247
|
-
it "should not be the same as the first generated value" do
|
248
|
-
@another_username.should_not == @username
|
249
|
-
end
|
250
|
-
end
|
251
|
-
end
|
252
|
-
|
253
|
-
describe "a factory with a default strategy specified" do
|
254
|
-
it "should generate instances according to the strategy" do
|
255
|
-
Factory(:post).should be_kind_of(Hash)
|
256
|
-
end
|
257
|
-
end
|
258
|
-
|
259
|
-
it "should raise Factory::SequenceAbuseError" do
|
260
|
-
lambda {
|
261
|
-
Factory(:sequence_abuser)
|
262
|
-
}.should raise_error(FactoryGirl::SequenceAbuseError)
|
263
|
-
end
|
264
|
-
|
265
|
-
describe "an instance with callbacks" do
|
266
|
-
it "should run the after_stub callback when stubbing" do
|
267
|
-
@user = Factory.stub(:user_with_callbacks)
|
268
|
-
@user.first_name.should == 'Stubby'
|
269
|
-
end
|
270
|
-
|
271
|
-
it "should run the after_build callback when building" do
|
272
|
-
@user = Factory.build(:user_with_callbacks)
|
273
|
-
@user.first_name.should == 'Buildy'
|
274
|
-
end
|
275
|
-
|
276
|
-
it "should run both the after_build and after_create callbacks when creating" do
|
277
|
-
@user = Factory(:user_with_callbacks)
|
278
|
-
@user.first_name.should == 'Buildy'
|
279
|
-
@user.last_name.should == 'Createy'
|
280
|
-
end
|
281
|
-
|
282
|
-
it "should run both the after_stub callback on the factory and the inherited after_stub callback" do
|
283
|
-
@user = Factory.stub(:user_with_inherited_callbacks)
|
284
|
-
@user.first_name.should == 'Stubby'
|
285
|
-
@user.last_name.should == 'Double-Stubby'
|
286
|
-
end
|
287
|
-
end
|
288
|
-
end
|
data/spec/acceptance/models.rb
DELETED
@@ -1,48 +0,0 @@
|
|
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
|
44
|
-
|
45
|
-
module Admin
|
46
|
-
class Settings
|
47
|
-
end
|
48
|
-
end
|