Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect require.Error() docs #1609

Open
andig opened this issue Jun 8, 2024 · 4 comments
Open

Incorrect require.Error() docs #1609

andig opened this issue Jun 8, 2024 · 4 comments
Labels
bug pkg-require Change related to package testify/require

Comments

@andig
Copy link

andig commented Jun 8, 2024

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 andig added the bug label Jun 8, 2024
@hendrywiranto
Copy link
Contributor

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.

@andig
Copy link
Author

andig commented Jun 11, 2024

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()
}

@hendrywiranto
Copy link
Contributor

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.

@brackendawson brackendawson changed the title Misleading Error() docs Incorrect require.Error() docs Oct 9, 2024
@brackendawson
Copy link
Collaborator

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 brackendawson added the pkg-require Change related to package testify/require label Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug pkg-require Change related to package testify/require
Projects
None yet
Development

No branches or pull requests

3 participants