Add additional default RSpec error information?

I test a lot of bad lines when checking as follows:

['0', '3:a', 'xx:11', '-1', '-3:00', 'h', 'h2', 'h2h', 'm', 'm10', 'm10m', '2hm', 'h2m', 'hm2', '2m10h', '2m10m', '2h10h'].each do |input| FactoryGirl.build(:time_record, duration_string: input).should have(1).error_on('duration_string'), "Tested value was '#{input}'" end 

Unfortunately, when one of them fails, the message expected 1 error on :duration_string, got 0 does not tell me which one failed. I know that I can pass a second argument that appears instead of the default message, for example:

 x.should have(1).error_on('duration_string'), "Tested value was '#{input}'" 

But this hides the original message. Is there a way to add only my own message to the original instead of replacing it?

Thanks.

+4
source share
2 answers

I solved it like this:

 ['0', '3:a', 'xx:11', '-1', '-3:00', 'h', 'h2', 'h2h', 'm', 'm10', 'm10m', '2hm', 'h2m', 'hm2', '2m10h', '2m10m', '2h10h'].each do |input| it "does not accept #{input}" do FactoryGirl.build(:time_record, duration_string: input).should have(1).error_on('duration_string'), "Tested value was '#{input}'" end end 

This way you also get cheaper to calculate your statistics .;)

+1
source

You can catch and reraise with another message:

 ['0', '3:a', 'xx:11', '-1', '-3:00', 'h', 'h2', 'h2h', 'm', 'm10', 'm10m', '2hm', 'h2m', 'hm2', '2m10h', '2m10m', '2h10h'].each do |input| begin FactoryGirl.build(:time_record, duration_string: input).should have(1).error_on('duration_string'), "Tested value was '#{input}'" rescue RSpec::Expectations::ExpectationNotMetError => e e.message << "failed at #{input}" raise e end end 
+5
source

All Articles