This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
fabrication_spec.rb
351 lines (246 loc) · 7.27 KB
/
fabrication_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
require 'spec_helper'
describe Fabrication do
context 'static fields' do
let(:person) { Fabricate(:person, :last_name => 'Awesome') }
let(:location) { Fabricate(:location) }
let(:dog) do
Fabricate(:dog) do
name nil
end
end
it 'has the default first name' do
person.first_name.should == 'John'
end
it 'has an overridden last name' do
person.last_name.should == 'Awesome'
end
it 'generates a fresh object every time' do
Fabricate(:person).should_not == person
end
it "has the latitude" do
location.lat.should == 35
end
it "has the longitude" do
location.lng.should == 40
end
it "handles nil values" do
dog.name.should be_nil
end
end
context 'block generated fields' do
let(:person) { Fabricate(:person) }
it 'has a last name' do
person.last_name.should be
end
it 'has an age' do
person.age.should be
end
it 'has 10 shoes' do
person.shoes.should == (1..10).map { |i| "shoe #{i}" }
end
end
context "when referring to other fabricators" do
let(:person) { Fabricate(:person) }
it "has the latitude" do
person.location.lat.should == 35
end
it "has the longitude" do
person.location.lng.should == 40
end
context "with a count" do
context "of one" do
let(:greyhound) do
Fabricate(:greyhound) do
locations(:count => 1)
end
end
it "should have one location" do
greyhound.locations.size.should == 1
greyhound.locations.first.lat.should == 35
greyhound.locations.first.lng.should == 40
end
end
context "of two" do
let(:greyhound) do
Fabricate(:greyhound) do
locations(:count => 2)
end
end
it "should have two locations" do
greyhound.locations.size.should == 2
greyhound.locations.each do |loc|
loc.lat.should == 35
loc.lng.should == 40
end
end
end
end
end
context 'with the generation parameter' do
let(:person) do
Fabricate(:person) do
first_name "Paul"
last_name { |person| "#{person.first_name}#{person.age}" }
end
end
it 'evaluates the fields in order of declaration' do
person.last_name.should == "Paul"
end
end
context 'multiple instance' do
let(:person1) { Fabricate(:person, :first_name => 'Jane') }
let(:person2) { Fabricate(:person, :first_name => 'John') }
it 'person1 is named Jane' do
person1.first_name.should == 'Jane'
end
it 'person2 is named John' do
person2.first_name.should == 'John'
end
it 'they have different last names' do
person1.last_name.should_not == person2.last_name
end
end
context 'with a specified class name' do
let(:someone) { Fabricate(:someone) }
before do
Fabricator(:someone, :class_name => :person) do
first_name "Paul"
end
end
it 'generates the person as someone' do
someone.first_name.should == "Paul"
end
end
context 'with an active record object' do
before { TestMigration.up }
after { TestMigration.down }
before(:all) do
Fabricator(:company) do
name { Faker::Company.name }
divisions!(:count => 4)
after_create { |o| o.update_attribute(:city, "Jacksonville Beach") }
end
Fabricator(:other_company, :from => :company) do
divisions(:count => 2)
after_build { |o| o.name = "Crazysauce" }
end
end
let(:company) { Fabricate(:company, :name => "Hashrocket") }
let(:other_company) { Fabricate(:other_company) }
it 'generates field blocks immediately' do
company.name.should == "Hashrocket"
end
it 'generates associations immediately when forced' do
Division.find_all_by_company_id(company.id).count.should == 4
end
it 'executes after build blocks' do
other_company.name.should == 'Crazysauce'
end
it 'executes after create blocks' do
company.city.should == 'Jacksonville Beach'
end
it 'overrides associations' do
Fabricate(:company, :divisions => []).divisions.should == []
end
it 'overrides inherited associations' do
other_company.divisions.count.should == 2
Division.count.should == 2
end
end
context 'with a mongoid document' do
let(:author) { Fabricate(:author) }
it "sets the author name" do
author.name.should == "George Orwell"
end
it 'generates four books' do
author.books.map(&:title).should == (1..4).map { |i| "book title #{i}" }
end
end
context 'with a parent fabricator' do
context 'and a previously defined parent' do
let(:ernie) { Fabricate(:hemingway) }
it 'has the values from the parent' do
ernie.books.count.should == 4
end
it 'overrides specified values from the parent' do
ernie.name.should == 'Ernest Hemingway'
end
end
context 'and a class name as a parent' do
let(:greyhound) { Fabricate(:greyhound) }
it 'has the breed defined' do
greyhound.breed.should == 'greyhound'
end
it 'does not have a name' do
greyhound.name.should be_nil
end
end
end
describe '.clear_definitions' do
before(:all) do
Fabrication.clear_definitions
end
after(:all) do
Fabrication::Support.find_definitions
end
it 'should not generate authors' do
Fabrication::Fabricator.schematics.has_key?(:author).should be_false
end
end
context 'when defining a fabricator twice' do
it 'throws an error' do
lambda { Fabricator(:author) {} }.should raise_error(Fabrication::DuplicateFabricatorError)
end
end
context "when defining a fabricator for a class that doesn't exist" do
it 'throws an error' do
lambda { Fabricator(:your_mom) }.should raise_error(Fabrication::UnfabricatableError)
end
end
context 'when generating from a non-existant fabricator' do
it 'throws an error' do
lambda { Fabricate(:your_mom) }.should raise_error(Fabrication::UnknownFabricatorError)
end
end
context 'defining a fabricator without a block' do
before(:all) do
class Widget; end
Fabricator(:widget)
end
it 'works fine' do
Fabricate(:widget).should be
end
end
describe "Fabricate with a block" do
let(:person) do
Fabricate(:person) do
age nil
first_name "Paul"
last_name { "Elliott" }
end
end
it 'uses the class matching the passed-in symbol' do
person.kind_of?(Person).should be_true
end
it 'has the correct first_name' do
person.first_name.should == 'Paul'
end
it 'has the correct last_name' do
person.last_name.should == 'Elliott'
end
it 'has the correct age' do
person.age.should be_nil
end
end
describe "Fabricate.attributes_for" do
let(:person) do
Fabricate.attributes_for(:person, :first_name => "John", :last_name => "Smith")
end
it 'has the first name as a parameter' do
person['first_name'].should == "John"
end
it 'has the last name as a parameter' do
person[:last_name].should == "Smith"
end
end
end