简体   繁体   English

Rails 4:使用MiniTest和Rspec

[英]Rails 4: using MiniTest AND Rspec

Is it possible and does anyone have experience using minitest and rspec? 是否有可能并且有没有人有使用minitest和rspec的经验? I'm just wondering if it's possible to slowly migrate from one to the other and have both get along in the mean time. 我只是想知道是否可以慢慢地从一个迁移到另一个并且同时相处。

I tried setting up some minitest tests in an rspec project and it's just failing silently at this point. 我尝试在rspec项目中设置一些最小的测试,但此时它只是默默地失败了。 I've even removed the rspec-rails gem but still 我甚至删除了rspec-rails gem

rake test my/test/file.rb

just silently returns. 只是默默地回来。

I just revisited this and it seems like Rspec and Minitest work fine together "out of the box" without needing minitest-rails. 我只是重新审视了这一点,似乎Rspec和Minitest在开箱即用的情况下可以很好地协同工作而不需要minitest-rails。

I'm not however using minitest/spec so I don't know about how that would integrate. 然而,我不是使用minitest / spec,所以我不知道它将如何整合。

My issue was that the project in question was explicitly setting up individual railties in config/application.rb just to exclude "rails/test_unit/railtie" which is great if you just want rspec. 我的问题是,有问题的项目是在config / application.rb中明确设置单个栏目,只是为了排除“rails / test_unit / railtie”,如果你只是想要rspec,那就太棒了。

I put it back to the default 我把它恢复到默认值

require 'rails/all'

and now both rspec specs run with 现在两个rspec规格一起运行

rake spec

and minitest tests run with 和最小的测试运行

rake test

I wanted both to run buy default with just 我希望两者都只运行购买默认值

rake

So I put this in my Rakefile 所以我把它放在我的Rakefile中

Rake::Task["default"].clear if Rake::Task.task_defined?("default")
task :default do
  puts "Starting specs"
  system('bundle exec rake spec')

  puts "Starting Minitest tests"
  system('bundle exec rake test')
end
require 'rake/testtask'
require 'rspec/core/rake_task'

Rake::TestTask.new(:test) do |t| 
  t.pattern = 'test/**/*_test.rb'
end

RSpec::Core::RakeTask.new(:spec) do |t| 
  t.pattern = Dir.glob('spec/**/*_spec.rb')
  # t.rspec_opts = '--format documentation'
end

task default: [:test, :spec]

It should be possible to do that. 应该可以这样做。 Minitest is part of the standard library, so there should not be any conflicts. Minitest是标准库的一部分,所以不应该有任何冲突。

If you want rake test to execute minitest, you will have to configure rails to use it. 如果您希望rake test执行minitest,则必须配置rails才能使用它。 This might be useful for doing it: https://github.com/blowmage/minitest-rails 这可能对此有用: https//github.com/blowmage/minitest-rails

Otherwise you could just execute a test directly ruby -Itest test/unit/whatever_test.rb 否则你可以直接执行测试ruby -Itest test/unit/whatever_test.rb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM