I am trying to assert a function that returns a nil, but i'm trying to assert nil but the following assertion doesn't make sense.
I am using the github.com/stretchr/testify/assert
framework to assert
passes: assert.Equal(t, meta == nil, true)
fails: assert.Equal(t, meta, nil)
I am not sure why this makes any sense. could someone help please.
Method being tested:
type Metadata struct {
UserId string
}
func GetAndValidateMetadata(metadata map[string]string) (*Metadata, error) {
userId _ := metadata["userId"]
if userId == "" {
return nil, errors.New("userId is undefined")
}
meta := Metadata{
UserId: userId,
}
return &meta, nil
}
testcase:
func TestValidation(t *testing.T) {
metadata := map[string]string{
"fakeUserId": "testUserId",
}
meta, err := GetAndValidateMetadata(metadata)
assert.Equal(t, meta == nil, true) <--- passes
assert.Equal(t, meta, nil) <--- fails
}
github.com/stretchr/testify/assert
package, but you may want to be explicit about that in your question, to lift any ambiguity.return
.assert.Nil
to test fornil
values