A long time reader, but the first poster here on SO :)
Over the past two days, I have set up FactoryGirl.
Yesterday, I changed some factories (mainly my Custom and brands ), replacing:
Language.find_or_create_by_code('en')
WITH
Language.find_by_code('en') || create(:language)
Because the first parameter creates a Language object with just the code attribute populated; and the second uses the factory language to create the object (and thus fills in all the attributes specified in the factory)
Now, when I run my test, it immediately fails on Factory.lint, stating that my factory users (and admin_user) are invalid. Returning the above code does not fix this, and the stack trace provided FactoryGirl.lintis useless.
When I comment on the lint function, my tests actually work just fine without any problems.
When I manually create a factory console in rails and use it .valid?on it, it returns true, so I don’t understand why lint considers my factories to be invalid.
My factory user is as follows:
FactoryGirl.define do
factory :user do
ignore do
lang { Language.find_by_code('en') || create(:language) }
end
login "test_user"
email "test_user@test.com"
name "Jan"
password "test1234"
password_confirmation "test1234"
role
brand
person
language { lang }
factory :admin_user do
association :role, factory: :admin
end
end
end
Here, the role , the person and the factory language are quite simple (just a few lines), but the factory brand uses the same language as the user, so I use the code in the ignore block, so FactoryGirl does not create 2 'en' language entries in my database.
Anyone have any ideas why I get this InvalidFactoryError and maybe talk about how to debug this?
UPDATE 1
, Factory..
factory, user_var_widget, :
factory :user_solar_widget, :class => 'UserWidget' do
sequence_number 2
user { User.find_by_login('test_user') }
widget { Widget.find_by_type('SolarWidget') || create(:solar_widget) }
end
create (: user), InvalidFactoryError Factory. , User factory , user_widgets. , , .
2
, User factory:
trait :with_widgets do
after(:create) do |user|
user.user_widgets << create(:user_solar_widget, user: user)
end
end
user_widgets - has_many .
user_solar_widget factory :
factory :user_solar_widget, :class => 'UserWidget' do
sequence_number 2
widget { Widget.find_by_type('SolarWidget') || create(:solar_widget) }
end
, :
create :user, :with_widgets
, , lint .