Search code examples
unit-testinggoassertgoland

Assertion for error contains and equals not working as expected


I am asserting an error from a function, I have provided such input for the function so that it will throw an error. I want to check if the error is correct.

Here is my function:

func validateK8ResourcesLength(resource prm.Resource) error {
    name := resource.GetName()
    namespace := resource.GetNamespace()
    k8sResourceName := fmt.Sprintf("%s-%s", namespace, name)
    if len(k8sResourceName) > common.K8sNameLength {
        return fmt.Errorf("Namespace (%s) and Name (%s) length combined must not be greater than 48 chars", namespace, name)
    }
    return nil
}

My testing code:

func TestK8testing(t *testing.T) {
    ctrl := gomock.NewController(t)
    defer ctrl.Finish()
    mockContext := prmmock.NewMockStepContext(ctrl)
    mockResource := prmmock.NewMockResource(ctrl)
    mockClient := prmmock.NewMockClient(ctrl)
    // testClient := fake.NewFakeClientWithScheme(runtime.NewScheme())
    lables := make(map[string]string)
    mockClient.EXPECT().GetResource(gomock.Any(), gomock.Any()).Return(mockResource, nil).AnyTimes()
    mockContext.EXPECT().Resource().Return(mockResource).AnyTimes()
    mockResource.EXPECT().GetKind().Return("data-movement-v1-nonexist").AnyTimes()
    mockResource.EXPECT().GetName().Return("test-dm-more-then-forty-eight-charactors-this-should-fail").AnyTimes()
    mockResource.EXPECT().GetNamespace().Return("ns-more-then-forty-eight-charactors-this-should-fail").AnyTimes()
    mockResource.EXPECT().GetLabels().Return(lables).AnyTimes()
    mockResource.EXPECT().GetID().Return(prm.NewResourceID("hcc-dm", "data-movement-v1", "test-dm")).AnyTimes()

    err := validateK8ResourcesLength(mockResource)
    assert.Error(t, err)
    if assert.Error(t, err) {
        //assert.Contains(t, err, "length combined must not be greater")
        assert.Equal(t, "Namespace (ns-more-then-forty-eight-charactors-this-should-fail) and Name (test-dm-more-then-forty-eight-charactors-this-should-fail) length combined must not be greater than 48 chars", err)
    }
}

Here is the error I am getting:

--- FAIL: TestK8testing (0.00s)
/Users/ngupta59/hcc-dm/pkg/dm-agent/cmd/action_validate_dmjob_test.go:296: 
        Error Trace:    action_validate_dmjob_test.go:296
        Error:          Not equal: 
                        expected: string("Namespace (ns-more-then-forty-eight-charactors-this-should-fail) and Name (test-dm-more-then-forty-eight-charactors-this-should-fail) length combined must not be greater than 48 chars")
                        actual  : *errors.errorString(&errors.errorString{s:"Namespace (ns-more-then-forty-eight-charactors-this-should-fail) and Name (test-dm-more-then-forty-eight-charactors-this-should-fail) length combined must not be greater than 48 chars"})
        Test:           TestK8testing

What is wrong with my code? Also my assert.contains is not working.


Solution

  • The error builtin type is an interface:

    type error interface {
        Error() string
    }
    

    So if you want to check the error value, you will actually need to call the Error() string method.

    This resource explains the basics: https://blog.golang.org/error-handling-and-go

    And the Go 1.13 extension to the basics: https://blog.golang.org/go1.13-errors