Reputation: 13195
I'm testing a lot of bad strings on a validation 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|
FactoryGirl.build(:time_record, duration_string: input).should have(1).error_on('duration_string'), "Tested value was '#{input}'"
end
Sadly, when one of them fails, the message expected 1 error on :duration_string, got 0
doesn't tell me, which one failed. I know that I can pass a second argument that is displayed instead of the default message like this:
x.should have(1).error_on('duration_string'), "Tested value was '#{input}'"
But this hides the original message. Is there a way to only append my own message to the original instead of replacing it?
Thank you.
Upvotes: 4
Views: 304
Reputation: 13195
I solved it this way:
['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 cheaply a higher specs count for your statistics. ;)
Upvotes: 2
Reputation: 21096
You can catch and reraise exception 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
Upvotes: 6