関連付けされたファクトリーでエラーが起きる可能性がある
spring
またはzeus
のようなrailsプレリーダーでrspecを実行している時、以下のような関連付けされたファクトリーを作成する場合は、ActiveRecord::AssociationTypeMismatch
エラーが発生する可能性がある。
When running RSpec with a Rails preloader such as spring or zeus, it's possible to encounter an ActiveRecord::AssociationTypeMismatch error when creating a factory with associations, as below:
FactoryBot.define do
factory :united_states, class: "Location" do
name { 'United States' }
association :location_group, factory: :north_america # associationで関連付けする、関連付けされたファクトリーも指定する
end
factory :north_america, class: "LocationGroup" do
name { 'North America' }
end
end
上のエラーの解決法
二つの可能な解決策はプレローダーなしのスイートのどちらか一方を実行させることまたは次のようにrspec.cofugurationにFactoryBot.reload
を追加することです。
The two possible solutions are to either run the suite without the preloader, or to add FactoryBot.reload to the RSpec configuration, like so:
RSpec.configure do |config|
config.before(:suite) { FactoryBot.reload }
end