user3814030
user3814030

Reputation: 1283

undefined method `create' in Rails RSpec

I have installed FactoryBot and trying to use it with RSpec.

scenario 'User signs in' do
  create :user, email: '[email protected]', password: 'testpassword'
  visit '/users/sign_in'

  fill_in 'Email', with: '[email protected]'
  fill_in 'Password', with: 'testpassword'
end

and I am getting the following error.

Failure/Error: create :user, email: '[email protected]', password: 'testpassword'
 NoMethodError:
   undefined method `create' for #<RSpec::ExampleGroups::UserSignIn:0x007fe6324816b8>

Upvotes: 25

Views: 17933

Answers (3)

hama99o
hama99o

Reputation: 81

go to /spec/rails_helper.rb and search for Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } and uncomment it

Upvotes: 1

yesnik
yesnik

Reputation: 4695

We can find solution in factory_bot's documentation:

1) Create file /spec/support/factory_bot.rb:

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end

2) Edit /spec/rails_helper.rb to load all files in support directory:

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

Upvotes: 47

ik00ma
ik00ma

Reputation: 432

In my case I was missing config.include FactoryGirl::Syntax::Methods in rails_helper.rb

Upvotes: 16

Related Questions