MiniTest :: Spec为什么不具有wont_raise断言? [英] Why doesn't MiniTest::Spec have a wont_raise assertion?

查看:65
本文介绍了MiniTest :: Spec为什么不具有wont_raise断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby的Test::Unit具有assert_nothing_raised. Test::Unit已由 MiniTest 代替.为什么MiniTest的断言/期望与此没有任何平行之处?例如,您可以期望must_raise,但不能期望wont_raise.

Ruby's Test::Unit has assert_nothing_raised. Test::Unit has been replaced by MiniTest. Why don't MiniTest's assertions / expectations have anything parallel to this? For example you can expect must_raise but not wont_raise.

推荐答案

MiniTest确实在其Test :: Unit兼容性层中实现了assert_nothing_raised,但在其自己的测试(MiniTest::UnitMiniTest::Spec)中却实现了实施任何此类测试.程序员认为,原因是测试没有提出任何内容并不意味着要进行任何测试.您绝不会期望测试中会发生任何事情,除非您正在测试 for 异常.如果测试代码中发生意外(未捕获)异常,则测试将按顺序报告异常,并且您会知道自己有问题.

MiniTest does implement assert_nothing_raised in its Test::Unit compatibility layer, but in its own tests (MiniTest::Unit and MiniTest::Spec) it does not implement any test like this. The reason is, the programmer argues, that testing for nothing raised is not a test of anything; you never expect anything to be raised in a test, except when you are testing for an exception. If an unexpected (uncaught) exception occurs in the code for a test, you'll get an exception reported in good order by the test and you'll know you have a problem.

示例:

require 'minitest/autorun'

describe "something" do
  it "does something" do
    Ooops
  end
end

输出:

Run options: --seed 41521

# Running tests:

E

Finished tests in 0.000729s, 1371.7421 tests/s, 0.0000 assertions/s.

  1) Error:
test_0001_does_something(something):
NameError: uninitialized constant Ooops
    untitled:5:in `block (2 levels) in <main>'

1 tests, 0 assertions, 0 failures, 1 errors, 0 skips

这正是您想知道的.如果您期望什么都没有提出,那么您就没有得到,而且已经被告知.

Which is exactly what you wanted to know. If you were expecting nothing to be raised, you didn't get it and you've been told so.

因此,这里的参数是:不要使用assert_nothing_raised!这只是毫无意义的拐杖.例如,参见:

So, the argument here is: do not use assert_nothing_raised! It's just a meaningless crutch. See, for example:

https://github.com/seattlerb/minitest/issues/70

https://github.com/seattlerb/minitest/issues/159

http://blog.zenspider.com/blog/2012/01/assert_nothing_tested.html

另一方面,显然assert_nothing_raised对应于用户之间的一些直觉,因为许多人期望wont_raisemust_raise搭配使用,依此类推.特别是,一个人想着眼于断言上,而不仅仅是一个测试.幸运的是,MiniTest非常简单和灵活,因此,如果您要添加自己的例程,则可以.因此,您可以编写一种方法来测试没有异常,如果没有异常,则返回一个已知结果,现在您可以为该已知结果进行断言.

On the other hand, clearly assert_nothing_raised corresponds to some intuition among users, since so many people expect a wont_raise to go with must_raise, etc. In particular one would like to focus an assertion on this, not merely a test. Luckily, MiniTest is extremely minimalist and flexible, so if you want to add your own routine, you can. So you can write a method that tests for no exception and returns a known outcome if there is no exception, and now you can assert for that known outcome.

例如(我并不是说这是完美的,只是说明了这个主意):

For example (I'm not saying this is perfect, just showing the idea):

class TestMyRequire < MiniTest::Spec
  def testForError # pass me a block and I'll tell you if it raised
    yield
    "ok"
  rescue
    $!
  end
  it "blends" do
    testForError do
      something_or_other
    end.must_equal "ok"
  end
end

关键不是说这是好事还是坏事,而是MiniTest永远不会为您做这件事.

The point is not that this is a good or bad idea but that it was never the responsibility of MiniTest to do it for you.

这篇关于MiniTest :: Spec为什么不具有wont_raise断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆