Using RSpec, how can I check the results of an exception block to save

I have a method in which there is a start / save block. How to check the recovery unit using RSpec2?

class Capturer def capture begin status = ExternalService.call return true if status == "200" return false rescue Exception => e Logger.log_exception(e) return false end end end describe "#capture" do context "an exception is thrown" do it "should log the exception and return false" do c = Capturer.new success = c.capture ## Assert that Logger receives log_exception ## Assert that success == false end end end 
+6
source share
1 answer

Use should_receive and should be_false :

 context "an exception is thrown" do before do ExternalService.stub(:call) { raise Exception } end it "should log the exception and return false" do c = Capturer.new Logger.should_receive(:log_exception) c.capture.should be_false end end 

Also note that you should not get rid of Exception , but something more specific. Exception covers everything that is almost certainly not what you want. At best, you should save from StandardError , which is the default.

+8
source

Source: https://habr.com/ru/post/911446/


All Articles