I want to test a function, which does not return any value, but instead triggers other functions. While reading about testing, I found information that this is called a behavioral verification and that by mocking I can check what functions and in what order are triggered. However, I have a problem to implement the proper mocking technique for my code.
Let's consider a simple example of the following interface and struct (the example is very basic just to make it easier to explain):
type ExampleInterface interface {
DoSomething(arg int)
DoEvenMore(arg int)
AndEvenMore(arg int)
}
type ExampleStruct struct {
Id string
// Other fields
}
func (e *ExampleStruct) DoSomething(arg int) {
arg2 := arg * 2
e.DoEvenMore(arg2)
arg3 := arg * 3
e.AndEvenMore(arg3)
}
func (e *ExampleStruct) DoEvenMore(arg int){
fmt.Println("I did so many operations here using ", arg)
}
func (e *ExampleStruct) AndEvenMore(arg int) {
// Performing other operations on arg
}
Now, I want to test function DoSomething
. Since it does not return any value what I want to do is to test whether after calling this function with argument 3 the following chain of events happens: function DoEvenMore
is called once with argument 6
, and next function AndEvenMore
is called once with argument 9
I wrote the following mocking test:
func TestDoSomething(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockClient := mocks.NewMockExampleInterface(mockCtrl)
example := ExampleStruct("ExampleId")
gomock.InOrder(
mockClient.EXPECT().DoSomething(3).Return().Times(1)
mockClient.EXPECT().DoEvenMore(6).Return().Times(1)
mockClient.EXPECT().AndEvenMore(9).Return().Times(1)
)
example.DoSomething(3)
}
However, when I run this test I get the error: Unexpected call to *mocks.MockExampleInterface.DoSomething(..)
.
How I should properly perform the mocking in such example?
The two methods called by DoSomething
are concrete implementations, you cannot mock something like that in Go.
To achieve what you want DoSomething
would have to depend on an interface that defines those two methods and call those instead of the concrete ones.
type DoMorer interface {
DoEvenMore(arg int)
AndEvenMore(arg int)
}
type ExampleStruct struct {
Id string
// Other fields
}
func (e *ExampleStruct) DoSomething(arg int, dm DoMorer) {
arg2 := arg * 2
dm.DoEvenMore(arg2)
arg3 := arg * 3
dm.AndEvenMore(arg3)
}
With a setup like this, during testing, you can create a mock for the DoMorer
interface and pass that to DoSomething
.
DoMorer
doesn't have to be passed to as an argument, it could be a field of ExampleStruct
or a global, whatever suits your needs.