Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Error - Mock method with int and string parameters #19

Closed
alvalea opened this issue Dec 24, 2015 · 4 comments
Closed

Error - Mock method with int and string parameters #19

alvalea opened this issue Dec 24, 2015 · 4 comments

Comments

@alvalea
Copy link

alvalea commented Dec 24, 2015

Mocking a method with int and string parameters generates a mock that does not expect those types. However, if both parameters are of type string, it works properly.

output
alem@alem-vm:~/Workspace/go/src/alem/test1$ go test lib_test.go                                                                                                         
--- FAIL: TestMyThing (0.00s)
        controller.go:113: no matching expected call: *mock_mypackage.MockMyInterface.SomeMethod([1 second])
        controller.go:158: missing call(s) to *mock_mypackage.MockMyInterface.SomeMethod(is equal to 1, is equal to second)
        controller.go:165: aborting test due to missing call(s)
FAIL
FAIL    command-line-arguments  0.002s
lib.go
package mypackage

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

import (
  "testing"
  "alem/test1/lib_mock"
  "github.com/golang/mock/gomock"
)

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

  mockObj := mock_mypackage.NewMockMyInterface(mockCtrl)

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

  mockObj.SomeMethod(1, "second")
}
mockgen
mockgen -source=lib.go > lib_mock/lib_mock.go
@dsymonds
Copy link
Contributor

dsymonds commented Jan 3, 2016

Numeric types in Go are distinct types. An int is not the same as a uint64. When your test writes mockObj.EXPECT().SomeMethod(1, "second") then the 1 becomes a int (since the expectation methods take interface{} args to support matchers), and int(1) is not uint64(1).

Write mockObj.EXPECT().SomeMethod(uint64(1), "second") instead and it should work fine.

@dsymonds dsymonds closed this as completed Jan 3, 2016
@alvalea
Copy link
Author

alvalea commented Jan 4, 2016

Ok, thank you very much

@melkus
Copy link

melkus commented Nov 28, 2016

Why do not generate mock that expects int64 instead of interface{} ?
It should be easy, because you already generates real types for Mock object, but not for MockRecorder.
Is there any reason you could not allow int64 and other specific types?
It could be a flag for mockgen finally.

@drscre
Copy link

drscre commented Jul 31, 2018

@melkus Because you can use gomock.Any() or similar constructs instead of concrete values when declaring expectations

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants