testify icon indicating copy to clipboard operation
testify copied to clipboard

Incorrect require.Error() docs

Open andig opened this issue 1 year ago • 7 comments

Description

The error docs read:

// Error asserts that a function returned an error (i.e. not `nil`).
//
//	  actualObj, err := SomeFunction()
//	  if assert.Error(t, err) {
//		   assert.Equal(t, expectedError, err)
//	  }

This indicates that Error() has a bool return value which it hasn't. There is no way to check expectedError like this.

Expected behavior

Error docs should show actual usage. It would be nice if Error() actually allowed for checking the specific error value.

andig avatar Jun 08 '24 10:06 andig

hello @andig

is this the Error() that you meant? https://github.com/stretchr/testify/blob/master/assert/assertions.go#L1596 Error has a bool return value, please do elaborate more if I misunderstood your meanings.

hendrywiranto avatar Jun 11 '24 17:06 hendrywiranto

Thank you for the quick reply. Seems I was actually looking at require.Error(). Thinking about it, the if case doesn't really make sense for require, so only the comment is off?

// Error asserts that a function returned an error (i.e. not `nil`).
//
//	  actualObj, err := SomeFunction()
//	  if assert.Error(t, err) {
//		   assert.Equal(t, expectedError, err)
//	  }
func Error(t TestingT, err error, msgAndArgs ...interface{}) {
	if h, ok := t.(tHelper); ok {
		h.Helper()
	}
	if assert.Error(t, err, msgAndArgs...) {
		return
	}
	t.FailNow()
}

andig avatar Jun 11 '24 18:06 andig

yea I think so, but I checked the docs for require, they all used assert as example in the comments, I don't know if it's intended or not I think if we want to change we need to adjust all the comments on the package.

hendrywiranto avatar Jun 12 '24 04:06 hendrywiranto

The doc-string for Error needs to be made into one which works for both the assert and require packages. Especially after #1610 is merged.

I'd suggest simply removing the if statement and the Equal call from the example. Especially since EqualError exists.

brackendawson avatar Oct 09 '24 13:10 brackendawson

Hi @brackendawson,

I can fix this doc-string for assert and require. To reconfirm we want to update the doc not to have an example that uses if block and then Equal function


// Error asserts that a function returned an error (i.e. not `nil`).
//
//	  actualObj, err := SomeFunction()
//	  assert.Error(t, err) 
func Error(t TestingT, err error, msgAndArgs ...interface{}) bool {
	if err == nil {
		if h, ok := t.(tHelper); ok {
			h.Helper()
		}
		return Fail(t, "An error is expected but got nil.", msgAndArgs...)
	}

	return true
}

and similar change in https://github.com/stretchr/testify/blob/master/require/require.go#L280

architagr avatar Nov 02 '24 20:11 architagr

Yes. The change in require is automatic, just run go generate ./....

brackendawson avatar Nov 11 '24 20:11 brackendawson

Yes. The change in require is automatic, just run go generate ./....

@brackendawson, I have created pull request #1675, can you please help me with review on this.

architagr avatar Nov 12 '24 05:11 architagr