首页 > 解决方案 > 在 Rails 中从 Minitest 切换到 RSpec 时,如何让 `rails test`(或 `rake test`)运行 RSpec 测试?

问题描述

RSpec 添加到 Rails时,我们最终可以运行rails spec(或bundle exec rspec)运行 RSpec 测试。

然而,rails test令人困惑的是,仍然尝试运行(不存在的)Minitest 测试。如何将“测试” rake 任务配置为运行 RSpec 测试?

标签: ruby-on-railsrspecrakeminitest

解决方案


您可以像这样覆盖test任务:

# Add the code below in the Rakefile
require File.expand_path(‘config/application’, __dir__)

Rails.application.load_tasks
Rake::Task['test'].clear

task :test do
  Rake::Task['rspec’].invoke
end

另一种方式(我只在我的控制台中尝试了一个放置一些东西但它应该与 rspec 一起使用的任务):

# Add the code below in the Rakefile
require File.expand_path(‘config/application’, __dir__)

Rails.application.load_tasks
task(:test).clear.enhance([‘rspec’])

推荐阅读