質問編集履歴
2
全エラー文のリンクの添付
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,6 +11,9 @@
|
|
11
11
|
/home/toshiki23/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/factory_bot-5.0.2/lib/factory_bot/definition_proxy.rb:97:in `method_missing': undefined method 'key' in 'ae_param' factory (NoMethodError)
|
12
12
|
```
|
13
13
|
と表示されました。
|
14
|
+
|
15
|
+
[エラー全文](https://pastebin.com/CuRycYyz)はこちらになります。
|
16
|
+
|
14
17
|
以下、definition_proxy.rb、91行目からのコードとなります。
|
15
18
|
```
|
16
19
|
def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing
|
1
追加のコード
title
CHANGED
File without changes
|
body
CHANGED
@@ -26,6 +26,203 @@
|
|
26
26
|
end
|
27
27
|
```
|
28
28
|
|
29
|
+
definition_proxy.rbの内容は以下の通りです。
|
30
|
+
|
31
|
+
```
|
32
|
+
module FactoryBot
|
33
|
+
class DefinitionProxy
|
34
|
+
UNPROXIED_METHODS = %w(
|
35
|
+
__send__
|
36
|
+
__id__
|
37
|
+
nil?
|
38
|
+
send
|
39
|
+
object_id
|
40
|
+
extend
|
41
|
+
instance_eval
|
42
|
+
initialize
|
43
|
+
block_given?
|
44
|
+
raise
|
45
|
+
caller
|
46
|
+
method
|
47
|
+
).freeze
|
48
|
+
|
49
|
+
(instance_methods + private_instance_methods).each do |method|
|
50
|
+
undef_method(method) unless UNPROXIED_METHODS.include?(method.to_s)
|
51
|
+
end
|
52
|
+
|
53
|
+
delegate :before, :after, :callback, to: :@definition
|
54
|
+
|
55
|
+
attr_reader :child_factories
|
56
|
+
|
57
|
+
def initialize(definition, ignore = false)
|
58
|
+
@definition = definition
|
59
|
+
@ignore = ignore
|
60
|
+
@child_factories = []
|
61
|
+
end
|
62
|
+
|
63
|
+
def singleton_method_added(name)
|
64
|
+
message = "Defining methods in blocks (trait or factory) is not supported (#{name})"
|
65
|
+
raise FactoryBot::MethodDefinitionError, message
|
66
|
+
end
|
67
|
+
|
68
|
+
# Adds an attribute to the factory.
|
69
|
+
# The attribute value will be generated "lazily"
|
70
|
+
# by calling the block whenever an instance is generated.
|
71
|
+
# The block will not be called if the
|
72
|
+
# attribute is overridden for a specific instance.
|
73
|
+
#
|
74
|
+
# Arguments:
|
75
|
+
# * name: +Symbol+ or +String+
|
76
|
+
# The name of this attribute. This will be assigned using "name=" for
|
77
|
+
# generated instances.
|
78
|
+
def add_attribute(name, &block)
|
79
|
+
declaration = Declaration::Dynamic.new(name, @ignore, block)
|
80
|
+
@definition.declare_attribute(declaration)
|
81
|
+
end
|
82
|
+
|
83
|
+
def transient(&block)
|
84
|
+
proxy = DefinitionProxy.new(@definition, true)
|
85
|
+
proxy.instance_eval(&block)
|
86
|
+
end
|
87
|
+
|
88
|
+
# Calls add_attribute using the missing method name as the name of the
|
89
|
+
# attribute, so that:
|
90
|
+
#
|
91
|
+
# factory :user do
|
92
|
+
# name { 'Billy Idol' }
|
93
|
+
# end
|
94
|
+
#
|
95
|
+
# and:
|
96
|
+
#
|
97
|
+
# factory :user do
|
98
|
+
# add_attribute(:name) { 'Billy Idol' }
|
99
|
+
# end
|
100
|
+
#
|
101
|
+
# are equivalent.
|
102
|
+
#
|
103
|
+
# If no argument or block is given, factory_bot will first look for an
|
104
|
+
# association, then for a sequence, and finally for a trait with the same
|
105
|
+
# name. This means that given an "admin" trait, an "email" sequence, and an
|
106
|
+
# "account" factory:
|
107
|
+
#
|
108
|
+
# factory :user, traits: [:admin] do
|
109
|
+
# email { generate(:email) }
|
110
|
+
# association :account
|
111
|
+
# end
|
112
|
+
#
|
113
|
+
# and:
|
114
|
+
#
|
115
|
+
# factory :user do
|
116
|
+
# admin
|
117
|
+
# email
|
118
|
+
# account
|
119
|
+
# end
|
120
|
+
#
|
121
|
+
# are equivalent.
|
122
|
+
def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing
|
123
|
+
if args.empty?
|
124
|
+
__declare_attribute__(name, block)
|
125
|
+
elsif args.first.respond_to?(:has_key?) && args.first.has_key?(:factory)
|
126
|
+
association(name, *args)
|
127
|
+
else
|
128
|
+
raise NoMethodError.new(
|
129
|
+
"undefined method '#{name}' in '#{@definition.name}' factory",
|
130
|
+
)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# Adds an attribute that will have unique values generated by a sequence with
|
135
|
+
# a specified format.
|
136
|
+
#
|
137
|
+
# The result of:
|
138
|
+
# factory :user do
|
139
|
+
# sequence(:email) { |n| "person#{n}@example.com" }
|
140
|
+
# end
|
141
|
+
#
|
142
|
+
# Is equal to:
|
143
|
+
# sequence(:email) { |n| "person#{n}@example.com" }
|
144
|
+
#
|
145
|
+
# factory :user do
|
146
|
+
# email { FactoryBot.generate(:email) }
|
147
|
+
# end
|
148
|
+
#
|
149
|
+
# Except that no globally available sequence will be defined.
|
150
|
+
def sequence(name, *args, &block)
|
151
|
+
sequence = Sequence.new(name, *args, &block)
|
152
|
+
FactoryBot::Internal.register_inline_sequence(sequence)
|
153
|
+
add_attribute(name) { increment_sequence(sequence) }
|
154
|
+
end
|
155
|
+
|
156
|
+
# Adds an attribute that builds an association. The associated instance will
|
157
|
+
# be built using the same build strategy as the parent instance.
|
158
|
+
#
|
159
|
+
# Example:
|
160
|
+
# factory :user do
|
161
|
+
# name 'Joey'
|
162
|
+
# end
|
163
|
+
#
|
164
|
+
# factory :post do
|
165
|
+
# association :author, factory: :user
|
166
|
+
# end
|
167
|
+
#
|
168
|
+
# Arguments:
|
169
|
+
# * name: +Symbol+
|
170
|
+
# The name of this attribute.
|
171
|
+
# * options: +Hash+
|
172
|
+
#
|
173
|
+
# Options:
|
174
|
+
# * factory: +Symbol+ or +String+
|
175
|
+
# The name of the factory to use when building the associated instance.
|
176
|
+
# If no name is given, the name of the attribute is assumed to be the
|
177
|
+
# name of the factory. For example, a "user" association will by
|
178
|
+
# default use the "user" factory.
|
179
|
+
def association(name, *options)
|
180
|
+
if block_given?
|
181
|
+
raise AssociationDefinitionError.new(
|
182
|
+
"Unexpected block passed to '#{name}' association "\
|
183
|
+
"in '#{@definition.name}' factory",
|
184
|
+
)
|
185
|
+
else
|
186
|
+
declaration = Declaration::Association.new(name, *options)
|
187
|
+
@definition.declare_attribute(declaration)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def to_create(&block)
|
192
|
+
@definition.to_create(&block)
|
193
|
+
end
|
194
|
+
|
195
|
+
def skip_create
|
196
|
+
@definition.skip_create
|
197
|
+
end
|
198
|
+
|
199
|
+
def factory(name, options = {}, &block)
|
200
|
+
@child_factories << [name, options, block]
|
201
|
+
end
|
202
|
+
|
203
|
+
def trait(name, &block)
|
204
|
+
@definition.define_trait(Trait.new(name, &block))
|
205
|
+
end
|
206
|
+
|
207
|
+
def initialize_with(&block)
|
208
|
+
@definition.define_constructor(&block)
|
209
|
+
end
|
210
|
+
|
211
|
+
private
|
212
|
+
|
213
|
+
def __declare_attribute__(name, block)
|
214
|
+
if block.nil?
|
215
|
+
declaration = Declaration::Implicit.new(name, @definition, @ignore)
|
216
|
+
@definition.declare_attribute(declaration)
|
217
|
+
else
|
218
|
+
add_attribute(name, &block)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
```
|
225
|
+
|
29
226
|
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
|
30
227
|
|
31
228
|
また同じようなエラーの解決策を [Qiita](https://qiita.com/sabinuki/items/e64278ce775582f72634)で見つけたのですが該当箇所が異なり、未解決のままです。
|