mercredi 23 février 2022

Ruby RSpec for checking exception not passing

I have an rspec where I am testing if an exception thrown from method1 is rescued and raised again in method2 (method2 calls method1). But the RSpec seems to fail. I have debugged through the code and can see the exception being raised.


  it 'should check for exceptions raised' do
      allow(TestClass).to receive(:method1) do
        raise ::Test::CustomException.new(1, 'test.csv.gz', StandardError.new('test exception'))
      end
      expect(@test_class.method2).to raise_error(Test::CustomException)
  end


  method2
   tester["key_list"].each do |key|
        begin
        TestClass.method1(key, self.id) do |row|
          #do something
        end
        rescue Test::CustomException => exception
          raise ::Test::CustomException.new(self.id, key, exception)
        end
  end

  method1
     begin
         #do something
     rescue => exc
        raise ::Test::CustomException.new(id, key, exc)
     end
  end

CustomException as the name says is a custom defined exception that takes in 3 parameters

Error:

Failures:

  1. should check for exceptions raised Failure/Error: expect(@test_class.method2).to raise_error

    Test::CustomException: true test_class.rb:146:in `rescue in block method'

Aucun commentaire:

Enregistrer un commentaire