Rspec:如果最终引发异常,如何测试方法内容?

时间:2015-10-27 20:17:18

标签: ruby rspec

我有一个方法:

  def post
    ...
    if res.failed?
      SlackNotifier.notify("Failed", { :channel => "#bugs" })
      raise "Boom"
    end
    res
  end

在rspec中我试图对此进行测试,但它失败了RuntimeError,因为该方法最终会引发异常:

it 'posts to slack' do
  allow(SlackNotifier).to receive(:notify)
  subject.post
  expect(SlackNotifier).to have_received(:notify).with("Failed", { :channel => "#bugs" })
end

我如何阻止它因raise而失败?

1 个答案:

答案 0 :(得分:1)

对该主题进行了解雇加固: allow(subject).to receive(:raise)