Question

I am trying to create a factory on a model with field names that contain hyphens and I cannot figure out syntax that will allow the hyphens. I am using 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

I get this error

unexpected tSTRING_BEG, expecting keyword_do or '{' or '(' (SyntaxError)

Anyone know how to get around this?

Was it helpful?

Solution

FactoryGirl::DefinitionProxy defines method_missing to call add_attribute using the missing method's name, so you should be able to sub that in instead:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top