[英]How to add "config.include FactoryBot::Syntax::Methods" to rspec config block in spec_helper.rb?
如果我添加:
config.include FactoryBot::Syntax::Methods
在下面
RSpec.configure do |config|
然后运行 rspec,我看到这个错误:
/Users/perry_mac/rails_projects/mymri/spec/spec_helper.rb:21:in `block in': 未初始化常量 FactoryBot (NameError)
我的 gemfile.lock 可以在这个 pastebin中看到
我的 gemfile 可以在这个 pastebin中看到
如果我省略 Rspec.configure 语句,我的测试都运行良好。 我想使用缩写语法,但不确定我在这里做错了什么。
注意:FactoryBot 以前称为 FactoryGirl
知道了。
这个链接给我指明了方向。
所需的添加应在spec/support/factory_bot.rb
,它应如下所示:
# RSpec
# spec/support/factory_bot.rb
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
注意:FactoryBot 以前称为 FactoryGirl
您必须将此字符串添加到文件“spec/ RAILS _helper.rb”中,而不是“spec_helper.rb”中
还要确保在spec/support/factory_bot.rb
require 'factory_bot'
这就是我最终遇到的问题。
确保在require 'rspec/rails'
之后在spec/rails_helper.rb
包含require 'support/factory_girl'
spec/rails_helper.rb
require 'rspec/rails'
。
把它放在require 'spec_helper'
之后,我收到了这个错误。
这个答案是根据以前的评论和factory_bot编译和测试的。
spec/support/factory_bot.rb
文件。spec/support/factory_bot.rb
文件中:require 'factory_bot' RSpec.configure do |config| config.include FactoryBot::Syntax::Methods end
spec/rails_helper.rb
文件中添加require 'support/factory_bot.rb'
。我认为这里值得一提的是,以后如果使用 FactoryGirl,您将收到一条弃用消息:
The factory_girl gem has been deprecated and has been replaced by factory_bot. Please switch to factory_bot as soon as possible.
只是对未来尝试使用 FactoryGirl 的开发人员的一个说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.