Search code examples
ruby-on-railsrspecmongoidfactory-botrspec-rails

FactoryGirl creates incomplete model


Assume that I have a city model where:

class city
  field :full_name, type: String # San Francisco, CA, United States
  field :_id, type: String, overwrite: true, default: ->{ full_name }
end

Assume that I have a factory defined in /spec/factories/cities.rb:

FactoryGirl.define do
  factory :city do
    full_name 'San Francisco, CA, United States'
  end
end

Running the following code in one of the specs:

city_attrs = { full_name: 'San Francisco, CA, United States' }
City.create! city_attrs
=> #<City _id: San Francisco, CA, United States, full_name: "San Francisco, CA, United States">

FactoryGirl.create(:city)
=> #<City _id: , full_name: "San Francisco, CA, United States">

How do I fix this without adding the following code to the /spec/factories/cities.rb?

before(:create) do |city, evaluator|
  city.id = city.full_name
end

EDIT the solution is to stop using FactoryGirl and use Fabrication instead as recommended in this answer


Solution

  • You need to override the initialization of the model used by FactoryGirl:

    FactoryGirl.define do
      trait :explicit_initialize do
        initialize_with { new(attributes) }
      end
    
      factory :city, traits: [:explicit_initialize] do
        full_name 'San Francisco, CA, United States'
      end
    
    end