在字段名称中使用连字符定义模型上的工厂



我正在尝试在字段名称包含连字符的模型上创建一个工厂,但我无法弄清楚允许连字符的语法。 我正在使用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)

有人知道如何解决这个问题吗?

FactoryGirl::DefinitionProxy定义了使用缺少的方法名称调用add_attribute method_missing,因此您应该能够将其替换为:

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

最新更新