0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

関連付けたファクトリはエラーが起きる可能性がある

Posted at

関連付けされたファクトリーでエラーが起きる可能性がある

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?