MiniTest / Shoulda和Shoulda / Context报告测试方法?

时间:2015-11-19 22:36:20

标签: ruby minitest shoulda

我从rspec分支并进入MiniTest而且应该。我无法弄清楚测试方法如何适用于... ...

class TestKata <  MiniTest::Test
  context 'Kata#n00bify' do

    should 'replace "to" and "too" with the number 2, even if
            they are only part of a word (E.g. today = 2day)' do
      assert_equal '2', Kata.n00bify('too')
      assert_equal '2', Kata.n00bify('too')
      assert_equal '2day', Kata.n00bify('today')
    end

    should 'replace "for" and "fore" with the number 4' do
      assert_equal '4', Kata.n00bify('for')
      assert_equal '4', Kata.n00bify('fore')
    end

   # ...

  end
end

以下是测试结果:

Failure:
TestKata#test_: Kata#n00bify should replace "for" and "fore" with the number 4.  [/.../test_n00b.rb:31]
Minitest::Assertion: Expected: "4"
  Actual: nil


Failure:
TestKata#test_: Kata#n00bify should replace "to" and "too" with the number 2, even if
            they are only part of a word (E.g. today = 2day).  [/.../test_n00b.rb:25]
Minitest::Assertion: Expected: "2"
  Actual: nil

我不明白的是 - 测试方法在哪里?请注意测试结果中的TestKata#test_<nothing>:。对我来说似乎很难看。如果只喜欢上下文,就会爱上它。即:

Failure:
Kata#n00bify should ...

我错过了什么?感谢帮助!

1 个答案:

答案 0 :(得分:0)

Shoulda提供了使用should块定义测试方法的替代语法。 Minitest要求测试方法名称以字符串"test_"开头,因此Shoulda使用该约定动态定义每个块的方法。当您编写should "foo"之类的内容时,您会得到一个名为test_: TestClassName#foo的测试方法。

至于输出,有很多宝石实现了替代格式。你可能会试着看看那些功能最全,维护得最好的迷你记者。