Antarr Byrd
Antarr Byrd

Reputation: 26061

Factory referring to association in after create

I have a factory for an account that has a relationship with a product. When adding an after create callback it is referring to the product and not the account.

account factory

FactoryBot.define do
  factory :account do
    billing_method { 'monthly' }
    workflow_state { 'active' }
  end

  after :create, &:create_api_credential
end

product factory

FactoryBot.define do
  factory :product do
    ...
    account
  end
end

test

subject { create(:product) }
it 'has an account' do
  subject
  assert_not_nil(subject.account)
end

the error

 NoMethodError: undefined method `create_api_credential' for #<Product:0x00007fab7cefe2f8>

create_api_credential exist on account not product

Upvotes: 0

Views: 136

Answers (1)

nuaky
nuaky

Reputation: 2086

Did you try to move: after :create, &:create_api_credential to :account block?

factory :account do
  billing_method { 'monthly' }
  workflow_state { 'active' }

  after :create, &:create_api_credential
end

Upvotes: 1

Related Questions