Diego Fernandez
Diego Fernandez

Reputation: 11

Rails 5 Rspec Trait with associates

I have some doubt with rails 5.2.8 and rspec

To create the object and trait with array associated the rspec code is

let!(:transfer)             { Transfer.last }
let!(:transfer_method_type) { create(:transfer_method_type, :with_transfer_methods, transfer: transfer) }

And the factory as:

FactoryBot.define do 
  factory :transfer_method_type, class: "TransferMethodType" do
    association :transfer_methods, transfer

    transfer_method_type_name   { 'Other' }

    trait :with_transfer_methods do
        association :transfer,
        transfer_methods { 
            [
              create(:transfer_method, transfer: transfer),
              create(:transfer_method, transfer: transfer, option: 'Other 2')
            ]
        }
      end
  end
end

But have an error testing it

Transfer Method Type Tests Show, Update, Delete Deletes a Transfer Method Type

Failure/Error: let!(:transfer_method_type) { create(:transfer_method_type, :with_transfer_methods, transfer: transfer) }

ArgumentError:

Trait not registered: transfer

Not sure how to fix it

Upvotes: 0

Views: 103

Answers (1)

smathy
smathy

Reputation: 27971

The code association :transfer_methods, transfer is basically short for:

transfer_methods { [ create :transfer_method, transfer ] }

Ie. the transfer argument is being seen as the trait of the transfer_method factory. Unsurprisingly, there's no trait matching the Transfer object you're passing in as the transfer attribute.

Upvotes: 0

Related Questions