我需要断言返回的字符串是否包含一个或另一个子字符串。
我是这样写的:
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不过,我想知道如何用assert_equal或assert_match将其写成一行
assert_match( /(Pragmatic Store Order Confirmation) (There was an error with your payment)/, mail.subject )但我只是不知道它或regexp是如何工作的。希望我已经说清楚了。谢谢!
发布于 2020-12-02 22:35:15
你已经有了基本的想法,但是正则表达式是错误的。
/(Pragmatic Store Order Confirmation) (There was an error with your payment)/这将匹配“务实的商店订单确认-您的付款出现了错误”,并捕获了“务实的商店订单确认”和“您的付款有错误”。
如果您想匹配某物或其他东西,请使用|。
/(Pragmatic Store Order Confirmation|There was an error with your payment)/然而,与assert_include完全匹配更好。
subjects = [
'Pragmatic Store Order Confirmation',
'There was an error with your payment'
]
assert_include subjects, mail.subject这相当于subjects.include?(mail.subject)。
最后,人们应该质疑为什么测试不知道邮件将包含哪个主题行。这应该取决于产生邮件的原因。你的测试应该像..。
if payment_error
assert_equal 'There was an error with your payment', mail.subject
else
assert_equal 'Pragmatic Store Order Confirmation', mail.subject
endhttps://stackoverflow.com/questions/65116696
复制相似问题