testify icon indicating copy to clipboard operation
testify copied to clipboard

Add PanicsWithErrorIs assertion

Open jannisbaudisch opened this issue 2 years ago • 0 comments

Summary

Adds new assertion method to test if a paniced error has a specific error in its chain

Changes

  • Add PanicsWithErrorIs with tests

Motivation

See my issue #1350.

I have some code that wraps a database error with fmt.Errorf() and panics it. Now I want to assert that this error is paniced.

The current method PanicsWithError only compares the error message, but does not use a error chain comparison.

Example:

func foo(source err) {
    panic(fmt.Errorf("Wrapping error %w", source))
}

func Test(t *testing.T) {
    expectedError := errors.New("some error")
    require.PanicsWithErrorIs(t, expectedError, func() { foo(expectedError) }) // This functionality is missing
}

jannisbaudisch avatar Feb 27 '23 23:02 jannisbaudisch