Reputation: 45
I need to assert if a returned string contains one or another substring.
I'm writing it like this:
if mail.subject.include?('Pragmatic Store Order Confirmation')
assert_equal 'Pragmatic Store Order Confirmation', mail.subject
else
assert_equal 'There was an error with your payment', mail.subject
end
However I would like to know how to write it as one line with assert_equal or assert_match I tried
assert_match( /(Pragmatic Store Order Confirmation) (There was an error with your payment)/, mail.subject )
But i just cant figure out how it or regexp works. Hope I've made myself clear. Thanks!
Upvotes: 1
Views: 809
Reputation: 164699
You've got the basic idea, but the regex is wrong.
/(Pragmatic Store Order Confirmation) (There was an error with your payment)/
That will match "Pragmatic Store Order Confirmation There was an error with your payment" and capture "Pragmatic Store Order Confirmation" and "There was an error with your payment".
If you want to match something or something else, use a |
.
/(Pragmatic Store Order Confirmation|There was an error with your payment)/
However, an exact match is better done with assert_include
.
subjects = [
'Pragmatic Store Order Confirmation',
'There was an error with your payment'
]
assert_include subjects, mail.subject
That is equivalent to subjects.include?(mail.subject)
.
Finally, one should question why the test does not know which subject line the mail will have. It should depend on why the mail was generated. Your test should be something like...
if payment_error
assert_equal 'There was an error with your payment', mail.subject
else
assert_equal 'Pragmatic Store Order Confirmation', mail.subject
end
Upvotes: 2