Post

factorybotで関連テーブルの作成を簡易に行なっているのに引数を渡したい

解決

FactoryBot.define do factory :user_action_error do user user_action_history { association(:user_action_history, user: user) } end end

参考

Creating associations initialized via ‘build’ no longer works in FactoryBot 5.0 · Issue #1255 · thoughtbot/factory_bot

事象

FactoryBot.define do factory :user_action_error do user user_action_history end end

のように、has_manyhas_onebelongs_to で関連されているテーブルを作成する宣言があった。

しかし、sns_action_historyにuserを渡す必要があり修正が必要。 build or create(:user_action_history) だとuser_action_errorで指定したbuild or createで実行して欲しいので要件を満たさない。

This post is licensed under CC BY 4.0 by the author.