FactoryBotの関連付けでエラーになる

ruby on rails 5.2

実現したいこと

FactoryBotで関連付けを正しく設定したい

発生している問題・エラーメッセージ

# /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/attribute_assigner.rb:16:in `block (2 levels) in object' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/attribute_assigner.rb:15:in `each' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/attribute_assigner.rb:15:in `block in object' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/attribute_assigner.rb:14:in `tap' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/attribute_assigner.rb:14:in `object' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/evaluation.rb:13:in `object' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/strategy/create.rb:9:in `result' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/factory.rb:43:in `run' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/factory_runner.rb:29:in `block in run' # /usr/local/bundle/gems/activesupport-5.2.5/lib/active_support/notifications.rb:170:in `instrument' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/factory_runner.rb:28:in `run' # /usr/local/bundle/gems/factory_bot-6.0.2/lib/factory_bot/strategy_syntax_method_registrar.rb:28:in `block in define_singular_strategy_method' # ./spec/requests/apis/v2/hoge.rb:5:in `block (3 levels) in <top (required)>' Finished in 0.62091 seconds (files took 15.52 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/requests/apis/v2/hoge.rb:15 # hogehoge piyopiyo

1対多のモデルをFactoryBotで実装してるところです。

FactoryBotでbelongs_toとhas_manyの関係にあるやつ
上記を参考に無限ループにならないようしたつもりですが、エラーとなります。
※エラーメッセージは大量のせいか?青字だけで肝心の赤字が見えない状態。

該当のソースコード

rspec

※コメントアウトを外すとエラーになる
/spec/factories/smart_lock_providers.rb

ruby

12FactoryBot.define do3 factory :smart_lock_provider do4 smart_lock_organizations{5 [FactoryBot.build(:smart_lock_organization, smart_lock_provider: nil)]6 }7 # ↓が問題の部分8 # smart_lock_doors{9 # [FactoryBot.build(:smart_lock_door, smart_lock_provider: nil)]10 # }11 end12end

/spec/factories/smart_lock_organizations.rb

ruby

1FactoryBot.define do2 factory :smart_lock_organization do3 smart_lock_provider 4 key { 'key' }5 name { 'name' }6 end7end

/spec/factories/smart_lock_doors.rb

ruby

1FactoryBot.define do2 factory :smart_lock_door do3 smart_lock_provider 4 smart_lock_organization 5 key { 'xxxxxx' }6 name { 'xxxxxx' }7 end8end

これを実行するとエラーになる
/spec/requests/apis/v2/hoge.rb

ruby

1require 'rails_helper'2 3RSpec.describe 'hogehoge', type: :request do4 describe "piyopiyo" do5 let!(:smart_lock_door) { create(:smart_lock_door) }6 it do7 expect(1 + 2).to eq 38 end9 end10end11

model

ruby

1class SmartLockProvider < ActiveRecord::Base2 has_many :smart_lock_organizations3 has_many :smart_lock_doors4end5

ruby

1class SmartLockOrganization < ActiveRecord::Base2 belongs_to :smart_lock_provider3 has_many :smart_lock_doors4end

ruby

1class SmartLockDoor < ActiveRecord::Base 2 belongs_to :smart_lock_provider, optional: true3 belongs_to :smart_lock_organization, optional: true4end

試したこと

/spec/factories/smart_lock_providers.rbのコメントアウト部分を外すと、
letにおいて、親から作っても、子から作ってもエラーになる事を確認。

ruby

1 let!(:smart_lock_provider) { create(:smart_lock_provider) }

ruby

1 let!(:smart_lock_organization) { create(:smart_lock_organization) }

ruby

1 let!(:smart_lock_door) { create(:smart_lock_door) }

逆にコメントアウトすると以下の場合でもエラーは出ない事を確認。

つまりコメントアウト部分の記述が問題だとはわかりましたが、どこが悪いか分からずにいます。

コメントを投稿

0 コメント