# Changing Test::Unit::Assertion messages

**URL:** <https://www.ruby-forum.com/t/changing-test-assertion-messages/111731>\
**Category:** Ruby\
**Created:** [August 10, 2007, 6:29am UTC](https://www.ruby-forum.com/t/changing-test-assertion-messages/111731 "2007-08-10T06:29:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![curt\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b77776/32.png) [@curt\_s](https://www.ruby-forum.com/u/curt_s)\
**Post date:** [August 10, 2007, 6:29am UTC](https://www.ruby-forum.com/t/changing-test-assertion-messages/111731/1 "2007-08-10T06:29:34Z")

</div>

In many of my tests, I do something along the lines of

```
 assert_match(/foobar/, content)

```

When the assertion fails, it prints out something resembling the  
inspect of the content. In some cases, it’s much more readable to print  
something different. How do I do that for this one case, but leave the  
default behaviour for other cases in the program.

I thought it would be as simple as overriding the inspect method on  
content, but that doesn’t do the trick.

I’ve been through the internals of Test::Unit::Assertion, and it seems  
to frustrate this effort.

cjs

---

<div class="post-metadata">

**Author:** ![curt\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b77776/32.png) [@curt\_s](https://www.ruby-forum.com/u/curt_s)\
**Post date:** [August 10, 2007, 6:45am UTC](https://www.ruby-forum.com/t/changing-test-assertion-messages/111731/2 "2007-08-10T06:45:35Z")

</div>

On Fri, 10 Aug 2007, Curt S. wrote:

> I thought it would be as simple as overriding the inspect method on  
> content, but that doesn’t do the trick.

Never mind, this does actually work if I set it in a different place.  
There’s some weird thing happening between defining inspect on an object  
inside or outside a block to which a custom assert method yields, but I  
can’t be bothered to debug it.

cjs

---

<div class="post-metadata">

**Author:** ![curt\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b77776/32.png) [@curt\_s](https://www.ruby-forum.com/u/curt_s)\
**Post date:** [August 10, 2007, 9:20am UTC](https://www.ruby-forum.com/t/changing-test-assertion-messages/111731/3 "2007-08-10T09:20:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![curt\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b77776/32.png) [@curt\_s](https://www.ruby-forum.com/u/curt_s)\
**Post date:** [August 11, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/changing-test-assertion-messages/111731/4 "2007-08-11T13:50:32Z")

</div>

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.com/23tlu5

```

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.
