我在使用Minitest的Spec语法和Rails 5的ActiveSupport :: TestCase时遇到了一些非常意外的行为。
这是使用Minitest的仅红宝石测试:
class MyTest < Minitest::Test
describe "outer loop" do
describe "inner loop" do
it "works" do
assert true
end
end
it "still works" do
assert true
end
it "sti" do
assert true
end
end
def test_no_work
assert false
end
end
结果如下所示:
...F
1) Failure:
MyTest#test_no_work [my_test.rb:21]
Failed assertion, no message given.
4 runs, 4 assertions, 1 failures, 0 errors, 0 skips
但是,在测试rails模型并继承ActiveSupport :: TestCase时,我得到以下内容:
.F
Failure:
BlahTest::outer loop#test_no_work [/blah/test/models/blah_test.rb:21]
Expected false to be truthy.
..F
Failure:
BlahTest::outer loop::inner loop#test_no_work [/blah/test/models/blah_test.rb:21]
Expected false to be truthy.
F
Failure:
BlahTest#test_no_work [/blah/test/models/blah_test.rb:21]
Expected false to be truthy.
6 runs, 6 assertions, 3 failures, 0 errors, 0 skips
完全相同的测试代码,第一行除外:
class BlahTest < ActiveSupport::TestCase
这是一个错误还是我错过了什么?
更新:
这是我的test_helper.rb供参考:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'minitest/spec'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
extend MiniTest::Spec::DSL
register_spec_type(self) do |desc|
desc < ActiveRecord::Base if desc.is_a?(Class)
end
end
我根据以下链接对其进行了修改:http://www.agustinvinao.com/rails-5-minitest-spec-fixtures这似乎与此处描述的描述类似:http://blowmage.com/2013/07/08/minitest-spec-rails4,除此之外:
class << self
remove_method :describe
end
应该修复由ActiveSupport :: TestCase和Minitest :: Spec定义的#describe
。但是,在Rails 5中,此块会中断,并出现以下错误:
`remove_method': method `describe' not defined in Class (NameError)