Rails RSpec 中未定义的方法“create”

2024-02-27

我已经安装了 FactoryBot 并尝试将其与 RSpec 一起使用。

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

  fill_in 'Email', with: 't[email protected] /cdn-cgi/l/email-protection'
  fill_in 'Password', with: 'testpassword'
end

我收到以下错误。

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

我们可以找到解决方案Factory_bot 的文档 https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#rspec:

1)创建文件/spec/support/factory_bot.rb:

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

2) Edit /spec/rails_helper.rb加载所有文件support目录:

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails RSpec 中未定义的方法“create” 的相关文章

随机推荐