undefined方法`create'track spec.

use*_*030 13 rspec factory-bot

我已经安装了工厂女孩并尝试使用它与规格.

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
Run Code Online (Sandbox Code Playgroud)

我收到以下错误.

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

yes*_*nik 19

我们可以在factory_bot的文档中找到解决方案:

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

RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end
Run Code Online (Sandbox Code Playgroud)

2)编辑/spec/rails_helper.rb以加载支持目录中的所有文件:

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


Ill*_*zma 11

在我的情况下,我失踪config.include FactoryGirl::Syntax::Methodsrails_helper.rb