【问题标题】:Rspec: How to test method contents if it eventually raises exception?Rspec:如果最终引发异常,如何测试方法内容?
【发布时间】:2015-10-27 20:17:17
【问题描述】:

我有一个方法:

  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 而失败?

【问题讨论】:

    标签: ruby rspec


    【解决方案1】:

    对主题的加注修复了它: allow(subject).to receive(:raise)

    【讨论】:

      猜你喜欢
      • 2019-03-12
      • 2019-03-09
      • 2015-11-28
      • 2018-05-19
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多