工厂指的是创建后的关联

Factory referring to association in after create

我有一个与产品有关系的帐户的工厂。添加创建后回调时,它指的是产品而不是帐户。

账号工厂

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

  after :create, &:create_api_credential
end

产品工厂

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

测试

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

错误

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

create_api_credential 存在于帐户而非产品中

您是否尝试移动: after :create, &:create_api_credential:account 块?

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

  after :create, &:create_api_credential
end