在Ruby gem上工作,并尝试在RSpec中使用FactoryBot。
我在support/factory_bot.rb
里有这个
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.find_definitions
end
end
在spec_helper.rb
中
require 'support/factory_bot'
当我尝试运行spec rake任务时,我得到这个错误:
support/factory_bot.rb:2:in `block in <top (required)>': uninitialized constant FactoryBot (NameError)
我遗漏了什么?当我使用旧的factory_girl gem时,这个方法工作得很好,但是它已经被重命名为factory_bot。谢谢!!
发布于 2018-01-04 16:59:17
多伊尔。这里犯了一个愚蠢的错误,用bundle exec rake spec
代替rake spec
解决了这个问题。
我还不得不在support/factory_bot.rb
的顶部添加require 'factory_bot'
发布于 2018-03-23 03:03:17
概述以防您从头开始执行此操作
installation rspec
details here (基本上是将gem
添加到Gemfile
,然后运行bundle install
)
在rails项目rails g rspec:install
中初始化RSPEC
创建新文件您的spec/support/factory_bot.rb
添加以下基本代码:
require 'factory_bot'
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
end
# RSpec without Rails
RSpec.configure do |config|
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.find_definitions
end
end
在spec/rails_helper.rb
上添加引用
require 'support/factory_bot'
以及删除任何未使用的fixture
引用,如下面的config.use_transactional_fixtures = true
应该就是这样了!最后在rspec默认文件夹中运行任何你想要的spec
文件,例如:spec/features/my_action_spec.rb
spec/models/my_model_spec.rb
spec/task/my_task_spec.rb
或者全部运行并根据您的设置进行检查
rspec
rails rspec
bundle exec rspec
我希望这篇文章能帮助某个人完成整个RSPEC
+ FactoryBot
Gem
的安装过程
发布于 2021-03-04 23:14:53
我也遇到过类似的问题。我通过删除默认测试套件( MiniTest )解决了这个问题。当您创建rails应用程序并打算使用rspec和factory_bot时,请在命令行中使用以下代码:
rails new myapp -T
希望这能对xP有所帮助
https://stackoverflow.com/questions/48091582
复制相似问题