问题描述
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::Unit
和MiniTest::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_raise
与must_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断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!