Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use FactoryBot in ActionMailer::Preview

I used to have the following

require 'factory_girl'
require_relative '../../support/factories/users.rb'

class UserMailerPreview < ActionMailer::Preview
  def invitation
    user = FactoryGirl.build(:user)
    UserMailer.invitation(user)
  end
end

and it was working with FactoryGirl 4.8

Now, I have updated to FactoryBot 4.8.2

    require 'factory_bot'
    require_relative '../../support/factories/users.rb'

    class UserMailerPreview < ActionMailer::Preview
      def invitation
        user = FactoryBot.build(:user)
        UserMailer.invitation(user)
      end
    end

But I get No such file to load -- factory_bot.rb. What should I require?

Thanks for the help.

like image 303
Sig Avatar asked Oct 26 '17 02:10

Sig


1 Answers

You need to include the methods:

require 'factory_bot_rails'

class MyEmailPreview < ActionMailer::Preview

  include FactoryBot::Syntax::Methods

  def foo
    user = create(:user)
  end
end
  • factory_bot 6.2
like image 53
Kris Avatar answered Nov 03 '22 00:11

Kris