RSpec测试滚动条在出现错误的方法中调用

问题描述 投票:1回答:1

我能够测试是否已调用[C​​0],但是在也会引发错误的方法中调用它时,它会因为引发错误而失败,并且会跳过Rollbar.warning

Rollbar.warning

这是我正在测试的方法:

  context 'unauthorized' do
    before do
      allow(Rollbar).to receive(:warning)
    end

    it 'sends a warning to rollbar' do
      subject.send(:log_and_raise_error)

      expect(Rollbar).to have_received(:warning)
    end
  end

但是当我运行规格时,它失败:

  def log_and_raise_error
    Rollbar.warning(
      "Not authorized error accessing resource #{ResourceID}"
    )

    raise NotAuthorized
  end

关于如何解决该错误并仍然测试Rollbar的任何想法?

ruby-on-rails ruby rspec ruby-on-rails-5 rollbar
1个回答
0
投票

您可以期待该错误或对其进行挽救:

期待错误:

 1) Authorization unauthorized sends a warning to rollbar
     Failure/Error: subject.send(:log_and_raise_error)

     NotAuthorized

救援错误:

    it 'sends a warning to rollbar' do
      expect { subject.send(:log_and_raise_error) }.to raise_error NotAuthorized

      expect(Rollbar).to have_received(:warning)
    end

    it 'sends a warning to rollbar' do
      subject.send(:log_and_raise_error) rescue NotAuthorized

      expect(Rollbar).to have_received(:warning)
    end
© www.soinside.com 2019 - 2024. All rights reserved.