RSpec: How to write unit test case to receive an exception which is getting raised in private method

那年仲夏 提交于 2019-12-02 05:37:16

You could write something like:

expect(ActiveRecord::StaleObjectError).to receive(:new).and_call_original

Because you are rescue-ing the exception

Make sure to check https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers

expect(car_v2).to receive(:method1).and_raise(ActiveRecord::StaleObjectError)

Means that when car_v2 receives the method1 you won't call it but you'll raise an exception of type ActiveRecord::StaleObjectError. This is why you also receive the ArgumentError

With rspec you can check that a specific code raises an error (that is not handled in your case it is handled -> rescue...) like this:

expect { my_cool_method }.to raise_error(ErrorClass)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!