Ruby Rspec:如果最终引发异常,如何测试方法内容?
我有一个方法:Ruby Rspec:如果最终引发异常,如何测试方法内容?,ruby,rspec,Ruby,Rspec,我有一个方法: def post ... if res.failed? SlackNotifier.notify("Failed", { :channel => "#bugs" }) raise "Boom" end res end 在rspec中,我试图测试这一点,但它失败了,出现了运行时错误,因为该方法最终会引发异常: it 'posts to slack' do allow(SlackNotifier).to re
def post
...
if res.failed?
SlackNotifier.notify("Failed", { :channel => "#bugs" })
raise "Boom"
end
res
end
在rspec中,我试图测试这一点,但它失败了,出现了运行时错误
,因为该方法最终会引发异常:
it 'posts to slack' do
allow(SlackNotifier).to receive(:notify)
subject.post
expect(SlackNotifier).to have_received(:notify).with("Failed", { :channel => "#bugs" })
end
我该如何阻止它因为
raise
而失败?在主题上的stubing-raise修复了它:
允许(主题)。接收(:升起)