undefined method `create' in Rails RSpec
Asked Answered
D

3

25

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>
Decastere answered 20/11, 2015 at 14:44 Comment(1)
Try FactoryGirl.createTucket
E
47

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 }
Erastian answered 10/6, 2017 at 21:51 Comment(0)
M
16

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

Matri answered 2/10, 2016 at 9:19 Comment(0)
G
1

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

Ginelle answered 20/5, 2021 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.