From: Phlip Date: 2009-02-05T10:59:32+09:00 Subject: Re: custom assertion message for unit tests Will Parsons wrote: > I'm having trouble trying to figure out how to customize an assertion > message when running unit tests. In particular, I want to check the > permissions on a file, so I have something like: > > assert_equal(0755, File.stat(test_file).mode & 0777) > > If it fails, I get a message like: > > <493> expected but was > <420>. > > and of course I would like the expected and actual permissions to be > displayed in octal rather than decimal. How can I do that? (Unless if you use Ruby 1.8.7), install gem assert2, then write: assert{ '755' == (File.stat(test_file).mode & 0777).to_s(8) } When that fails, it prints out every expression, with its intermediate value, including those bitfields' decimal representations, and 1) Failure: test_assert_classic_message(Assert21Suite) [/cygdrive/c/Users/Phlip/.UNA/2/1/1/assert2/lib/assert2.rb:214:in `assert_' /cygdrive/c/Users/Phlip/.UNA/2/1/1/assert2/lib/assert2.rb:76:in `assert' test/assert2_suite.rb:403:in `test_assert_classic_message']: assert{ "755" == ( File.stat(test_file).mode() & 511 ).to_s(8) } --> false - should pass test_file --> "test/assert2_suite.rb" File.stat(test_file) --> # 33216 ( File.stat(test_file).mode() & 511 ) --> 448 ( File.stat(test_file).mode() & 511 ).to_s(8) --> "700". 29 tests, 115 assertions, 1 failures, 0 errors All assertions should reflect everything they possibly can at failure time, without too much excess verbiage when you write them. They should be easy to write and comprehensive when they fail. Assert{ 2.0 } maximizes your bang-buck ratio there! -- Phlip