Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Rspec:如果最终引发异常,如何测试方法内容?_Ruby_Rspec - Fatal编程技术网

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修复了它:
允许(主题)。接收(:升起)