[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Changing Test::Unit::Assertion messages

Curt Sampson

8/10/2007 4:29:00 AM

3 Answers

Curt Sampson

8/10/2007 4:44:00 AM

0

rking

8/10/2007 7:17:00 AM

0

I know you said, "Nevermind," but I wanted to add this bit:

Don't forget about the third parameter to the assert messages. It is
very useful for either giving hints about the meaning of the
assertion, or to diagnose what went wrong, either of which can be
helpful for posterity.

It's important to know that assert_match(pat,obj) essentially matches
`obj.to_s =~ pat`. The output gives the impression that it's matching
`obj.inspect =~ pat`, but that's just sort of an artifact of
AssertionMessage#convert.

Here's some code/output:

require 'test/unit'
class Sampson
def initialize
@riddle = '"Out of the eater, something to eat;' +
'out of the strong, something sweet."'
end
class PhilistineTest < Test::Unit::TestCase
def test_quiz
nazirite = Sampson.new
pat = %r/eater/
assert_match(pat, nazirite, "#{nazirite} !~ /#{pat}/")
end
end
end

Loaded suite /home/rking/src/scrap
Started
F
Finished in 0.047188 seconds.

1) Failure:
test_quiz(Sampson::PhilistineTest) [/home/rking/src/scrap.rb:36]:
#<Sampson:0xb7ca2c74> !~ /(?-mix:eater)/.
<#<Sampson:0xb7ca2c74
@riddle=
"\"Out of the eater, something to eat;out of the strong, something
sweet.\"">> expected to be =~
</eater/>.

1 tests, 1 assertions, 1 failures, 0 errors

See that line immediately after the test_quiz(...) line? That's my
custom-provided line. It's kind of buried, but having it is at least
clearer than the lines below, which are a little misleading.

HTH,
-rking

Phlip

8/11/2007 11:49:00 AM

0

rking wrote:

> assert_match(pat, nazirite, "#{nazirite} !~ /#{pat}/")

> See that line immediately after the test_quiz(...) line? That's my
> custom-provided line. It's kind of buried, but having it is at least
> clearer than the lines below, which are a little misleading.

That's why I wrote assert_raise_message:

http://tinyurl....

When inventing a new assertion - maybe an arbitrarily complex one - you
can't leave its user with "nil should not be nil". You should reflect
anything you know into the diagnostic. All assertions should take a
'message' field, for the user to add to the explanation.

When a test fails, maximizing the information output helps the user rapidly
decide whether to debug the failure, or just revert the code.

--
Phlip
http://www.oreilly.com/catalog/9780...
"Test Driven Ajax (on Rails)"
assert_xpath, assert_javascript, & assert_ajax