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
.
相关问题
- Dependencies while implementing Mocking in Junit4
- How to unit test a reactive component where ngCont
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
相关文章
- How to replace file-access references for a module
- Ruby using wrong version of openssl
- How to mock methods return object with deleted cop
- What is a good way of cleaning up after a unit tes
-
EF6 DbSet
returns null in Moq - Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
If you need it:
And to use it:
This bothered me enough to dig into the MiniTest sources and provide an implementation in my
spec_helper.rb
file:Hope this proves helpful to someone else who also needs
wont_raise
. Cheers! :)MiniTest does implement
assert_nothing_raised
in its Test::Unit compatibility layer, but in its own tests (MiniTest::Unit
andMiniTest::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.Example:
Output:
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.
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
On the other hand, clearly
assert_nothing_raised
corresponds to some intuition among users, since so many people expect awont_raise
to go withmust_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):
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.