【问题标题】:MiniTest/Shoulda and Shoulda/Context reporting test methods?MiniTest/Should 和 Shoulda/Context 报告测试方法?
【发布时间】:2015-11-19 22:36:20
【问题描述】:

我正在从 rspec 分支出来,进入 MiniTest 和 shoulda。我无法弄清楚测试方法如何与 shoulda 一起使用......

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_&lt;nothing&gt;:。在我看来很难看。很想用应该的上下文来阅读它。即:

Failure:
Kata#n00bify should ...

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

【问题讨论】:

    标签: ruby minitest shoulda


    【解决方案1】:

    应该提供另一种语法来使用should 块定义测试方法。 Minitest 要求测试方法名称以字符串"test_" 开头,因此Shouda 使用该约定为每个块动态定义方法。当你写should "foo" 之类的东西时,你会得到一个名为test_: TestClassName#foo 的测试方法。

    至于输出,有大量的 gem 实现了替代格式。您可以尝试查看 minitest-reporters,它是功能最齐全且维护得最好的。

    【讨论】:

      猜你喜欢
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多