[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[ANN] assert{ 2.0 }

Phlip

2/12/2008 3:29:00 AM

Rubies:

The following RubyUnit assertions are now obsolete:

assert
assert_block
assert_equal
assert_instance_of
assert_kind_of
assert_operator
assert_match
assert_nil
assert_no_match
assert_not_equal
assert_not_nil

The next time you think to type any of them, use assert{ 2.0 } instead.
Put whatever you like inside the { block }. assert_equal(x, y) becomes
assert{ x == y }. assert_not_nil(x) becomes ... just assert{ x }! If the
last statement in that block fails, your test runner will reflect the
name and value of every variable in your assertion. Diagnosing assertion
failures has never been easier!

Get assert{ 2.0 } the usual way:

gem install assert2

Then require 'assert2' into your test suites.

Use deny{} for situations like assert_nil that must fail.

To provide even more commentary, when either assertion fails, pass a
string into either assert or deny, like this:

assert('my colleague made me do this'){ foo() < 42 }

Note: This works "best" with Ruby 1.8.6!

Feedback welcome, and happy testing!

--
Phlip
http://www.oreillynet.com/ruby/blog/2008/02/as...
1 Answer

Phlip

2/14/2008 3:35:00 AM

0

> gem install assert2

There's a new version, with a deceptively simple feature that will increase
the uses for this assertion.

Here's the (private) test for the

def test_catch_exceptions
x = 42

assert_flunked /RuntimeError.*gotcha.*42/m do
assert{ x; raise 'gotcha' }
end

assert_flunked /look out.*RuntimeError.*gotcha.*42/m do
assert('look out!'){ x; raise 'gotcha' }
end
end

If the asserted block raises an exception, the assertion traps it, reports
it, and then reflects all its variables anyway.

This might sound like a modest gain. The assertion remains useful even when
your code is crashing. However, trapping exceptions gives the assertion more
uses. Assertion failures are themselves exceptions. This implies you can
retrofit classic assertions like this:

assert do
assert_equal whatever, whatever
assert_not_nil whatever
assert etc
end

If any of them fail, the assertion will report the values of whatever and
etc.

This also implies one can wrap RSpec behavior definitions, with same
benefits when they fail.

> Feedback welcome, and happy testing!
>
> --
> Phlip
> http://www.oreillynet.com/ruby/blog/2008/02/as...