我正在尝试在包含连字符的字段名称的模型上创建工厂,我无法弄清楚允许连字符的语法。我正在使用Mongoid。
#model.rb
class MyModel
include Mongoid::Document
field :field1
field :"data-field2"
field :"data-field3"
end
#factories/my_model.rb
FactoryGirl.define do
factory :my_model do
field1 'some text'
data-field2 'some_element_classname'
data-field2 'some_other_element_classname'
end
end
我收到此错误
unexpected tSTRING_BEG, expecting keyword_do or '{' or '(' (SyntaxError)
任何人都知道如何解决这个问题?
答案 0 :(得分:2)
FactoryGirl::DefinitionProxy
定义method_missing
使用缺少的方法名称调用add_attribute
,因此您应该能够将其替换为:
FactoryGirl.define do
factory :my_model do
field1 'some text'
add_attribute(:"data-field2", 'some_element_classname')
add_attribute(:"data-field3") { # add_attribute with block }
end
end