Undefined method `create 'rails spec.

I installed a factory girl and tried to use it with a spec.

scenario 'User signs in' do create :user, email: 'test@example.com', password: 'testpassword' visit '/users/sign_in' fill_in 'Email', with: 'test@example.com' fill_in 'Password', with: 'testpassword' end 

and I get the following error.

 Failure/Error: create :user, email: 'test@example.com', password: 'testpassword' NoMethodError: undefined method `create' for #<RSpec::ExampleGroups::UserSignIn:0x007fe6324816b8> 
+8
rspec factory-bot
source share
2 answers

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

+7
source share

We can find a solution in the factory documentation :

1) Create the file / spec / support / factory _girl.rb:

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

2) Modify / spec / rails _helper.rb to upload all files to the support directory:

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

All Articles