undefined method `create' rails spec.

不问归期 提交于 2019-12-21 07:55:20

问题


I have installed factory girl and trying to use it with 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 am getting the following error.

Failure/Error: create :user, email: 'test@example.com', password: 'testpassword'
 NoMethodError:
   undefined method `create' for #<RSpec::ExampleGroups::UserSignIn:0x007fe6324816b8>

回答1:


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 }



回答2:


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



来源:https://stackoverflow.com/questions/33829440/undefined-method-create-rails-spec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!