Search code examples
ruby-on-railsfactory-botrspec-railsruby-on-rails-5

FactoryGirl disable linting for some factories


So I have this piece of code in my rails_helper

config.before(:suite) do
    begin
      FactoryGirl.lint
  end

Which is causing me headaches. I have a User class which can have several attached profiles like this :

class User
  has_one :student_profile, class_name: Student
  has_one :employee_profile, class_name: Employee
end

Now the thing is, during User registration, I need to send a different email layout depending on which type of user is registering (I am classifying their profiles, and depending on the "stronger" profile, I switch to an appropriate layout.

I have overriden the devise mailer to add a layout based on the main_profile type

def layout_for_user(user)
    case user.main_profile // user.employee_profile || user.student_profile || user
    when Employee
      'layouts/mailer/company'
    when Student
      'layouts/mailer/student'
    else
      fail ArgumentError, 'Unknown layout for profile'
    end
end

In my registration process, I make sure I build at least one profile type before saving the user/sending the confirmation.

But it would seem that Factory Girl tries to build & save every type of factory, so I am getting lots of user - Unknown layout for profile (ArgumentError)

Is there a way to tell FactoryGirl.lint to skip some factories ? It doesn't make sense to have a User without any profile for example, but then an error is still generated

# rspec/factories/user.eb
FactoryGirl.define do
  factory :user do
    ...

  trait(:student) do

    after(:build) do |user, evaluator|
      user.student_profile = build(:student_profile,
        user: user)
      end
    end

  factory :student_user, traits: [:student]
end

Here my user factory is some sort of abstract factory that should never be instanciated alone (otherwise it causes the error explained above) any way to solve that ? I am thinking of commenting this line FactoryGirl.lint otherwise ?


Solution

  • You do not have to run lint with default arguments. To disable linting for some - factories can be filtered beforehand:

    FactoryGirl.lint(FactoryGirl.factories.reject{|f| f.name == :some_abstract_factory })