我有这样的方法
def className
def method_name
some code
rescue
some code and error message
end
end
那么,如何写下 rspec 来测试救援块..?
如果你想拯救,就意味着你期待some code
提出某种异常。
您可以使用RSpec 存根伪造实施并强制发生错误。假设执行块包含一个可能引发的方法
def method_name
other_method_that_may_raise
rescue => e
"ERROR: #{e.message}"
end
将存根挂钩到规范中的该方法
it " ... " do
subject.stub(:other_method_that_may_raise) { raise "boom" }
expect { subject.method_name }.to_not raise_error
end
您还可以通过测试结果来检查救援处理程序
it " ... " do
subject.stub(:other_method_that_may_raise) { raise "boom" }
expect(subject.method_name).to eq("ERROR: boom")
end
不用说,您应该提出一个可能由实际实现引发的错误,而不是一般错误
{ raise FooError, "boom" }
并只拯救那个Error
,假设这是相关的。
附带说明一下,在 Ruby 中,您可以使用以下命令定义一个类:
class ClassName
not
def className
就像你的例子一样。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)