Reputation: 147
I'm using the FactoryGirl gem to create a the following factory:
FactoryGirl.define do
conta_origem = FactoryGirl.create(:conta_pessoa_fisica)
conta_destino = ''
tipos = LogTransacao.tipos.keys
params = { conta_origem: conta_origem, conta_destino: conta_destino, tipo: tipos[0] }
codigo_transacional = LogTransacaoHelper::Gerador.codigo_alphanumerico(params)
factory :log_transacao_carga, class: LogTransacao do
codigo_transacional { codigo_transacional }
tipo { tipos[0] }
estornado { false }
valor { 0 }
conta_origem_id { conta_origem.id }
conta_origem_valor_antes_transacao { conta_origem.saldo }
end
end
conta.rb
FactoryGirl.define do
factory :conta do
nome { Faker::Name.name }
saldo { 0 }
status { 1 }
factory :conta_pessoa_fisica do
pessoa_type { 'PessoaFisica' }
pessoa_id { FactoryGirl.create(:pessoa_fisica).id }
end
end
end
So, I'm getting the following error:
find': Factory not registered: pessoa_fisica
This problem does no occur when I use the FactoryGirl.create inside of the factory ':log_transacao_carga'.
conta_origem_id { FactoryGirl.create(:conta_pessoa_fisica).id }
However, if I do this, I cannot use the same factory to populate the conta_origem_valor_antes_transacao. So, I need to create this factory before the log_transacao_carga factory.
Could someone help me, please?
Upvotes: 1
Views: 43
Reputation: 3019
This problem does no occur when I use the FactoryGirl.create inside of the factory ':log_transacao_carga'.
log_transacao_carga
works because you are passing the class name to it (LogTransacao
).
Any particular reason you are doing nested factories vs. trait
? You can use FactoryGirl's trait
like so:
FactoryGirl.define do
factory :conta do
nome { Faker::Name.name }
saldo { 0 }
status { 1 }
trait :conta_pessoa_fisica do
pessoa_type { 'PessoaFisica' }
pessoa_id { FactoryGirl.create(:pessoa_fisica).id }
end
end
end
and then call it with build(:conta, :conta_pessoa_fisica
). This way it reads my better and you are able to inherit parent factory attributes and add-on / edit the ones you need in a specific context.
Check out FactoryGirl docs: http://www.rubydoc.info/github/thoughtbot/factory_girl/FactoryGirl/Syntax/Methods
Here is a good intro video from the creators of FactoryGirl: https://thoughtbot.com/upcase/videos/factory-girl
Upvotes: 0