How to write down the rspec to test rescue block.?

后端 未结 2 1278
余生分开走
余生分开走 2021-01-13 22:49

I have method like this

def className  
  def method_name
    some code  
  rescue  
    some code and error message  
  end  
end

So, How

2条回答
  •  情书的邮戳
    2021-01-13 23:27

    you can stub with return error

    for example you have class with method like this :

    class Email
      def self.send_email
        # send email
      rescue
        'Error sent email'
      end
    end
    

    so rspec for raising error is

    context 'when error occures' do
      it 'should return error message' do
        allow(Email).to receive(:send_email) { err }
        expect(Email.send_email).to eq 'Error sent email brand'
      end
    end
    

提交回复
热议问题