Why does "FactoryGirl.lint" give an InvalidFactoryError?

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               # belongs_to :role
    brand              # belongs_to :brand
    person             # belongs_to :person
    language { lang }  # belongs_to :language

    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') } # || create(: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
  # removed the user line
  widget { Widget.find_by_type('SolarWidget') || create(:solar_widget) }
end

, :

create :user, :with_widgets  

, , lint .

+4
1

FactoryGirl.lint - . :

# Based on https://github.com/thoughtbot/factory_girl/
#          wiki/Testing-all-Factories-(with-RSpec)
require 'rails_helper'

RSpec.describe FactoryGirl do
  described_class.factories.map(&:name).each do |factory_name|
    describe "#{factory_name} factory" do
      it 'is valid' do
        factory = described_class.build(factory_name)
        expect(factory)
          .to be_valid, -> { factory.errors.full_messages.join("\n") }
      end
    end
  end
end
+14

Source: https://habr.com/ru/post/1535701/


All Articles