在Ruby on rails项目中,我希望在代码中断言异常消息,以确保它失败的原因是正确的,并提到重要的细节.
下面是一个总是引发以下问题的函数:
class Quiz < ApplicationRecord
def self.oops
raise ArgumentError.new("go away")
end
end
测试:
require "test_helper"
class QuizTest < ActiveSupport::TestCase
test "error message" do
assert_raises(ArgumentError, match: /whatever/) do
Quiz.oops()
end
end
end
当我运行bin/rake test
时,它通过了,但我预计它会失败,因为实际的错误消息与assert_raises
中的match
不匹配.
如何捕获错误消息并针对其进行断言?