[Rspec-devel] Failure messages - customize per "should"assertion

itsme213 itsme213 at hotmail.com
Sun May 28 11:01:44 EDT 2006


From: "aslak hellesoy" <aslak.hellesoy at gmail.com>

>> Is there a way to customize the failure message for each "should" 
>> assertion?
>>
>> e.g. I have
>>
>> context 'object graph'
>>  specify 'every activity should have an output' do
>>   Activity.instances.each {|a| a.outputs.should_not_be_empty }
>>  end

Note that the "should" is separated from the "specify" by some logic (in 
this case, a loop). In such cases the "should" string may not be specific 
enough for a given failure.

>>
> Can you provide a code example of how you would like to customize
> error messages?

Yes. Instead of this:

1)
ExpectationNotMetError in 'object graph every activity should have an 
output'
[] should not be empty

I would like to see some this:
ExpectationNotMetError in 'object graph every activity should have an 
output'
ACTIVITY A72
  [] should not be empty

Possible API for this passing in a block that will yield the detailed 
string:
context 'object graph'
 specify 'every activity should have an output' do
  Activity.instances.each {|a| a.outputs.should_not_be_empty {a.name} }
 end

> Why would you like to customize them?

In cases where the "should" is separated from the "specify" by some logic 
(in this case, a loop), the "should" string may not be specific enough for a 
given failure.

Btw, this request may be related to nested (or otherwise structured) 
contexts, which may be a better and more general solution if it can be kept 
simple. I will post that separately.

Thanks!



More information about the Rspec-devel mailing list