jeudi 18 octobre 2018

Rspec: Test an exception which is not handled

In my public method #recalculate, calling the private method1. This method throw exception 'StandardError'. I want to test this scenario, however getting an error.

Note: Don't want to handle an exception.

def recalculate
  method_1
end

private
def method_1
    ## do some calculation
    raise StandardError.new("Test")
end

Rspec Test case:

  it "Test" do
    expect { @product.recalculate.recalculate }.to raise_error(StandardError)
    @product.recalculate
  end

1) Product.Test 
  Failure/Error: @product.recalculate
   StandardError:
     Test
   (required)>'

Finished in 1.39 seconds 1 example, 1 failure

Aucun commentaire:

Enregistrer un commentaire