Factory_bot: ActiveRecord::AssociationTypeMismatch on belongs_to

Created on 26 Jul 2017  路  9Comments  路  Source: thoughtbot/factory_bot

Hello,

This was asked a million times but the only working solution I found was to set config.cache_classes = false in config/environments/development.rb which is less than optimal for development as one could guess. I am also not using Spring.

What I basically do is:

u = FactoryGirl.create(:user)
o = FactoryGirl.create(:organization, user: u)
(...)
ActiveRecord::AssociationTypeMismatch: Country(#79985380) expected, got #<Country id: 9, name: "Country #9", created_at: "2017-07-26 11:33:04", updated_at: "2017-07-26 11:33:04"> which is an instance of Country(#62001400)

Is the only way to enable the class caching or am I doing something wrong?

organization.rb

class Organization < ApplicationRecord
(...)
  has_many :users
  belongs_to :user
  belongs_to :country
(...)
end

spec/factories/organizations.rb

# frozen_string_literal: true

FactoryGirl.define do
  factory :organization do
    sequence(:name) { |n| "Organization ##{n}" }
    # https://4programmers.net/Forum/Inne/191628-generowanie_numeru_nip
    sequence(:nip) do |n|
      srand(n)
      sum = 10
      nip = []
      while sum == 10
        nip = Array.new(9) { rand(10) }
        sum = nip.zip([6, 5, 7, 2, 3, 4, 5, 6, 7]).map{ |a| a.inject(&:*) }.inject(&:+) % 11
      end
      (nip << sum).join
    end
    address "Przyk艂adowa 2/8"
    postcode "50-123"
    city "Wroc艂aw"
    status :created
    association :country
    association :user
  end
end

country.rb

# frozen_string_literal: true

class Country < ApplicationRecord
  validates :name, presence: true, uniqueness: true
  has_many :users
  has_many :projects
end

spec/factories/countries.rb

FactoryGirl.define do
  factory :country do
    sequence(:name) { |n| "Country ##{n}" }
  end
end

Most helpful comment

Hey @Quintasan! A few suggestions/ideas:

  • Have you tried setting your factories with singular filenames? Like factories/organization.rb, factories/country.rb and so on?
  • Have you tried simplifying the association within the factory (as per example)?
    rb factory :post do # ... author end
  • Have you tried explicitly declaring your factory (as per example)?
    rb factory :post do # ... association :author, factory: :user end

All 9 comments

Hey @Quintasan! A few suggestions/ideas:

  • Have you tried setting your factories with singular filenames? Like factories/organization.rb, factories/country.rb and so on?
  • Have you tried simplifying the association within the factory (as per example)?
    rb factory :post do # ... author end
  • Have you tried explicitly declaring your factory (as per example)?
    rb factory :post do # ... association :author, factory: :user end

@gnclmorais this is happening to me and a few members of my team when we specifically change code and then run tests again. Without changing app code, you can run a spec over and over with no errors. A single code change and this error occurs until you spring stop (we're using spring).

Singular filenames, explicitly declaring factories...none of it seems to work. Any ideas? We are on rails 5.2.2 and factory_bot/factory_bot_rails 5.0.0. In test.rb, we have cache_classes = true

@travisofthenorth was this happening before you upgraded to 5.0.0, or is this new for you (I ask because we did make some changes to the way factory_bot_rails handles loading file definitions in 5.0.0)?

I should also mention that some version of this has been happening for a long time and and we have a documented workaround: https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#rails-preloaders-and-rspec

@composerinteralia it only started happening after the upgrade.

Wow, I did not know about this workaround. It seems to have fixed the problem for us! Thank you so much!

This should be fixed with Fixed with https://github.com/thoughtbot/factory_bot_rails/pull/330. I will try to get a factory_bot_rails release out next week.

@composerinteralia I'm still getting this error in development rails console (pry). I'm on rails 6 and 5.0.2 factory bot. Has a release been cut with this fix yet?

https://github.com/thoughtbot/factory_bot_rails/pull/329 went out in factory_bot_rails 5.0.2. You might be experiencing a different problem with a similar symptom. Are you able to share a sample application that demonstrates the problem?

@composerinteralia yes happy to do so and will open a new issue thanks

Was this page helpful?
0 / 5 - 0 ratings