[英]RSpec factories with FactoryBot 'uninitialized constant FactoryBot'
I am setting up request specs following this tutorial . 我将在本教程之后设置请求规范。 If I run the tests with
bundle exec rspec
I get the following error: 如果我使用
bundle exec rspec
运行测试, bundle exec rspec
收到以下错误:
An error occurred while loading ./spec/factories/entities_spec.rb.
Failure/Error:
FactoryBot.define do
factory :entity do
name { FFaker::Lorem.word }
end
end
NameError:
uninitialized constant FactoryBot
The tutorial only asks to add some configuration to the rails_helper.rb file. 本教程仅要求向rails_helper.rb文件添加一些配置。 I can't figure where I need to add a
require 'factory_bot'
. 我不知道我需要在哪里添加
require 'factory_bot'
。 If I add it to the top of the rails_helper.rb file, the error persists. 如果将其添加到rails_helper.rb文件的顶部,该错误仍然存在。 And if I add it to the factories/entity_spec.rb file, I get this error:
如果我将其添加到factory / entity_spec.rb文件,则会收到此错误:
An error occurred while loading ./spec/requests/entities_spec.rb.
Failure/Error:
factory :entity do
name { FFaker::Lorem.word }
end
FactoryBot::DuplicateDefinitionError:
Factory already registered: entity
只需将我的factories/*.rb
文件从例如factories/user_spec.rb
重命名为factories/user.rb
解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.