質問編集履歴
2
全エラー文のリンクの添付
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,6 +24,12 @@
|
|
24
24
|
|
25
25
|
と表示されました。
|
26
26
|
|
27
|
+
|
28
|
+
|
29
|
+
[エラー全文](https://pastebin.com/CuRycYyz)はこちらになります。
|
30
|
+
|
31
|
+
|
32
|
+
|
27
33
|
以下、definition_proxy.rb、91行目からのコードとなります。
|
28
34
|
|
29
35
|
```
|
1
追加のコード
test
CHANGED
File without changes
|
test
CHANGED
@@ -54,6 +54,400 @@
|
|
54
54
|
|
55
55
|
|
56
56
|
|
57
|
+
definition_proxy.rbの内容は以下の通りです。
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
module FactoryBot
|
64
|
+
|
65
|
+
class DefinitionProxy
|
66
|
+
|
67
|
+
UNPROXIED_METHODS = %w(
|
68
|
+
|
69
|
+
__send__
|
70
|
+
|
71
|
+
__id__
|
72
|
+
|
73
|
+
nil?
|
74
|
+
|
75
|
+
send
|
76
|
+
|
77
|
+
object_id
|
78
|
+
|
79
|
+
extend
|
80
|
+
|
81
|
+
instance_eval
|
82
|
+
|
83
|
+
initialize
|
84
|
+
|
85
|
+
block_given?
|
86
|
+
|
87
|
+
raise
|
88
|
+
|
89
|
+
caller
|
90
|
+
|
91
|
+
method
|
92
|
+
|
93
|
+
).freeze
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
(instance_methods + private_instance_methods).each do |method|
|
98
|
+
|
99
|
+
undef_method(method) unless UNPROXIED_METHODS.include?(method.to_s)
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
delegate :before, :after, :callback, to: :@definition
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
attr_reader :child_factories
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
def initialize(definition, ignore = false)
|
114
|
+
|
115
|
+
@definition = definition
|
116
|
+
|
117
|
+
@ignore = ignore
|
118
|
+
|
119
|
+
@child_factories = []
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
def singleton_method_added(name)
|
126
|
+
|
127
|
+
message = "Defining methods in blocks (trait or factory) is not supported (#{name})"
|
128
|
+
|
129
|
+
raise FactoryBot::MethodDefinitionError, message
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
# Adds an attribute to the factory.
|
136
|
+
|
137
|
+
# The attribute value will be generated "lazily"
|
138
|
+
|
139
|
+
# by calling the block whenever an instance is generated.
|
140
|
+
|
141
|
+
# The block will not be called if the
|
142
|
+
|
143
|
+
# attribute is overridden for a specific instance.
|
144
|
+
|
145
|
+
#
|
146
|
+
|
147
|
+
# Arguments:
|
148
|
+
|
149
|
+
# * name: +Symbol+ or +String+
|
150
|
+
|
151
|
+
# The name of this attribute. This will be assigned using "name=" for
|
152
|
+
|
153
|
+
# generated instances.
|
154
|
+
|
155
|
+
def add_attribute(name, &block)
|
156
|
+
|
157
|
+
declaration = Declaration::Dynamic.new(name, @ignore, block)
|
158
|
+
|
159
|
+
@definition.declare_attribute(declaration)
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
def transient(&block)
|
166
|
+
|
167
|
+
proxy = DefinitionProxy.new(@definition, true)
|
168
|
+
|
169
|
+
proxy.instance_eval(&block)
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
# Calls add_attribute using the missing method name as the name of the
|
176
|
+
|
177
|
+
# attribute, so that:
|
178
|
+
|
179
|
+
#
|
180
|
+
|
181
|
+
# factory :user do
|
182
|
+
|
183
|
+
# name { 'Billy Idol' }
|
184
|
+
|
185
|
+
# end
|
186
|
+
|
187
|
+
#
|
188
|
+
|
189
|
+
# and:
|
190
|
+
|
191
|
+
#
|
192
|
+
|
193
|
+
# factory :user do
|
194
|
+
|
195
|
+
# add_attribute(:name) { 'Billy Idol' }
|
196
|
+
|
197
|
+
# end
|
198
|
+
|
199
|
+
#
|
200
|
+
|
201
|
+
# are equivalent.
|
202
|
+
|
203
|
+
#
|
204
|
+
|
205
|
+
# If no argument or block is given, factory_bot will first look for an
|
206
|
+
|
207
|
+
# association, then for a sequence, and finally for a trait with the same
|
208
|
+
|
209
|
+
# name. This means that given an "admin" trait, an "email" sequence, and an
|
210
|
+
|
211
|
+
# "account" factory:
|
212
|
+
|
213
|
+
#
|
214
|
+
|
215
|
+
# factory :user, traits: [:admin] do
|
216
|
+
|
217
|
+
# email { generate(:email) }
|
218
|
+
|
219
|
+
# association :account
|
220
|
+
|
221
|
+
# end
|
222
|
+
|
223
|
+
#
|
224
|
+
|
225
|
+
# and:
|
226
|
+
|
227
|
+
#
|
228
|
+
|
229
|
+
# factory :user do
|
230
|
+
|
231
|
+
# admin
|
232
|
+
|
233
|
+
# email
|
234
|
+
|
235
|
+
# account
|
236
|
+
|
237
|
+
# end
|
238
|
+
|
239
|
+
#
|
240
|
+
|
241
|
+
# are equivalent.
|
242
|
+
|
243
|
+
def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing
|
244
|
+
|
245
|
+
if args.empty?
|
246
|
+
|
247
|
+
__declare_attribute__(name, block)
|
248
|
+
|
249
|
+
elsif args.first.respond_to?(:has_key?) && args.first.has_key?(:factory)
|
250
|
+
|
251
|
+
association(name, *args)
|
252
|
+
|
253
|
+
else
|
254
|
+
|
255
|
+
raise NoMethodError.new(
|
256
|
+
|
257
|
+
"undefined method '#{name}' in '#{@definition.name}' factory",
|
258
|
+
|
259
|
+
)
|
260
|
+
|
261
|
+
end
|
262
|
+
|
263
|
+
end
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
# Adds an attribute that will have unique values generated by a sequence with
|
268
|
+
|
269
|
+
# a specified format.
|
270
|
+
|
271
|
+
#
|
272
|
+
|
273
|
+
# The result of:
|
274
|
+
|
275
|
+
# factory :user do
|
276
|
+
|
277
|
+
# sequence(:email) { |n| "person#{n}@example.com" }
|
278
|
+
|
279
|
+
# end
|
280
|
+
|
281
|
+
#
|
282
|
+
|
283
|
+
# Is equal to:
|
284
|
+
|
285
|
+
# sequence(:email) { |n| "person#{n}@example.com" }
|
286
|
+
|
287
|
+
#
|
288
|
+
|
289
|
+
# factory :user do
|
290
|
+
|
291
|
+
# email { FactoryBot.generate(:email) }
|
292
|
+
|
293
|
+
# end
|
294
|
+
|
295
|
+
#
|
296
|
+
|
297
|
+
# Except that no globally available sequence will be defined.
|
298
|
+
|
299
|
+
def sequence(name, *args, &block)
|
300
|
+
|
301
|
+
sequence = Sequence.new(name, *args, &block)
|
302
|
+
|
303
|
+
FactoryBot::Internal.register_inline_sequence(sequence)
|
304
|
+
|
305
|
+
add_attribute(name) { increment_sequence(sequence) }
|
306
|
+
|
307
|
+
end
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
# Adds an attribute that builds an association. The associated instance will
|
312
|
+
|
313
|
+
# be built using the same build strategy as the parent instance.
|
314
|
+
|
315
|
+
#
|
316
|
+
|
317
|
+
# Example:
|
318
|
+
|
319
|
+
# factory :user do
|
320
|
+
|
321
|
+
# name 'Joey'
|
322
|
+
|
323
|
+
# end
|
324
|
+
|
325
|
+
#
|
326
|
+
|
327
|
+
# factory :post do
|
328
|
+
|
329
|
+
# association :author, factory: :user
|
330
|
+
|
331
|
+
# end
|
332
|
+
|
333
|
+
#
|
334
|
+
|
335
|
+
# Arguments:
|
336
|
+
|
337
|
+
# * name: +Symbol+
|
338
|
+
|
339
|
+
# The name of this attribute.
|
340
|
+
|
341
|
+
# * options: +Hash+
|
342
|
+
|
343
|
+
#
|
344
|
+
|
345
|
+
# Options:
|
346
|
+
|
347
|
+
# * factory: +Symbol+ or +String+
|
348
|
+
|
349
|
+
# The name of the factory to use when building the associated instance.
|
350
|
+
|
351
|
+
# If no name is given, the name of the attribute is assumed to be the
|
352
|
+
|
353
|
+
# name of the factory. For example, a "user" association will by
|
354
|
+
|
355
|
+
# default use the "user" factory.
|
356
|
+
|
357
|
+
def association(name, *options)
|
358
|
+
|
359
|
+
if block_given?
|
360
|
+
|
361
|
+
raise AssociationDefinitionError.new(
|
362
|
+
|
363
|
+
"Unexpected block passed to '#{name}' association "\
|
364
|
+
|
365
|
+
"in '#{@definition.name}' factory",
|
366
|
+
|
367
|
+
)
|
368
|
+
|
369
|
+
else
|
370
|
+
|
371
|
+
declaration = Declaration::Association.new(name, *options)
|
372
|
+
|
373
|
+
@definition.declare_attribute(declaration)
|
374
|
+
|
375
|
+
end
|
376
|
+
|
377
|
+
end
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
def to_create(&block)
|
382
|
+
|
383
|
+
@definition.to_create(&block)
|
384
|
+
|
385
|
+
end
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
def skip_create
|
390
|
+
|
391
|
+
@definition.skip_create
|
392
|
+
|
393
|
+
end
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
def factory(name, options = {}, &block)
|
398
|
+
|
399
|
+
@child_factories << [name, options, block]
|
400
|
+
|
401
|
+
end
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
def trait(name, &block)
|
406
|
+
|
407
|
+
@definition.define_trait(Trait.new(name, &block))
|
408
|
+
|
409
|
+
end
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
def initialize_with(&block)
|
414
|
+
|
415
|
+
@definition.define_constructor(&block)
|
416
|
+
|
417
|
+
end
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
private
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
def __declare_attribute__(name, block)
|
426
|
+
|
427
|
+
if block.nil?
|
428
|
+
|
429
|
+
declaration = Declaration::Implicit.new(name, @definition, @ignore)
|
430
|
+
|
431
|
+
@definition.declare_attribute(declaration)
|
432
|
+
|
433
|
+
else
|
434
|
+
|
435
|
+
add_attribute(name, &block)
|
436
|
+
|
437
|
+
end
|
438
|
+
|
439
|
+
end
|
440
|
+
|
441
|
+
end
|
442
|
+
|
443
|
+
end
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
```
|
448
|
+
|
449
|
+
|
450
|
+
|
57
451
|
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
|
58
452
|
|
59
453
|
|