diff --git a/assert/assertions.go b/assert/assertions.go index d7c16c5..348d5f1 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -832,11 +832,11 @@ func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, m // // Returns whether the assertion was successful (true) or not (false). func NoError(t TestingT, err error, msgAndArgs ...interface{}) bool { - if isNil(err) { - return true + if err != nil { + return Fail(t, fmt.Sprintf("Received unexpected error %q", err), msgAndArgs...) } - return Fail(t, fmt.Sprintf("Received unexpected error %q", err), msgAndArgs...) + return true } // Error asserts that a function returned an error (i.e. not `nil`). @@ -850,8 +850,11 @@ func NoError(t TestingT, err error, msgAndArgs ...interface{}) bool { func Error(t TestingT, err error, msgAndArgs ...interface{}) bool { message := messageFromMsgAndArgs(msgAndArgs...) - return NotNil(t, err, "An error is expected but got nil. %s", message) + if err == nil { + return Fail(t, "An error is expected but got nil. %s", message) + } + return true } // EqualError asserts that a function returned an error (i.e. not `nil`) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 15a04b8..12d8151 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -523,8 +523,26 @@ func TestNoError(t *testing.T) { False(t, NoError(mockT, err), "NoError with error should return False") + // returning an empty error interface + err = func() error { + var err *customError + if err != nil { + t.Fatal("err should be nil here") + } + return err + }() + + if err == nil { // err is not nil here! + t.Errorf("Error should be nil due to empty interface", err) + } + + False(t, NoError(mockT, err), "NoError should fail with empty error interface") } +type customError struct{} + +func (*customError) Error() string { return "fail" } + func TestError(t *testing.T) { mockT := new(testing.T) @@ -539,6 +557,20 @@ func TestError(t *testing.T) { True(t, Error(mockT, err), "Error with error should return True") + // returning an empty error interface + err = func() error { + var err *customError + if err != nil { + t.Fatal("err should be nil here") + } + return err + }() + + if err == nil { // err is not nil here! + t.Errorf("Error should be nil due to empty interface", err) + } + + True(t, Error(mockT, err), "Error should pass with empty error interface") } func TestEqualError(t *testing.T) {