golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.32k stars 607 forks source link

Confusing Error message with EXPECT() #128

Closed jtestard closed 7 years ago

jtestard commented 7 years ago

Mocking a method using EXPECT gives a confusing error messages when the call count for the method does not match the number of occurrences of this message in the code but the method signatures match.

A more descriptive error message would be most helpful.

Output
└──➤ go test lib_test.go
--- FAIL: TestMyThing (0.00s)
    controller.go:129: no matching expected call: *mock_mypackage.MockMyInterface.SomeMethod([1 second])
FAIL
FAIL    command-line-arguments  0.006s
lib.go
package mypackage

type MyInterface interface {
    SomeMethod(x int, y string)
}
lib_test.go
package mypackage

import (
    "github.com/golang/mock/gomock"
    "github.com/jtestard/devIssues/lib_mock"
    "testing"
)

func TestMyThing(t *testing.T) {
    mockCtrl := gomock.NewController(t)
    defer mockCtrl.Finish()

    mockObj := mock_mypackage.NewMockMyInterface(mockCtrl)

    mockObj.EXPECT().SomeMethod(1, "second")

    mockObj.SomeMethod(1, "second")
    mockObj.SomeMethod(1, "second")
}
mockgen
mockgen -source=lib.go > lib_mock/lib_mock.go
balshetzer commented 7 years ago

The error message with #129 would be:

--- FAIL: TestMyThing (0.00s)
        controller.go:148: Unexpected call to *lib_mock.MockMyInterface.SomeMethod([1 second]) at /home/ubuntu/workspace/src/test/lib_test.go:18 because: 
                Expected call at /home/ubuntu/workspace/src/test/lib_test.go:15 has already been called the max number of times.
FAIL