Mocking a Golang method that stores result in pointer argument

Sagar Sonwane
4 min readMar 11, 2021

The purpose of this blog is to show a way by which we can make a mock call to a method that takes a pointer as an argument.

When is Mocking Needed?

Mocking is done when you wish to invoke methods of a class that has external communication like database calls or rest calls. Through mocking, you can explicitly define the return value of methods without actually executing the method.

So, from the above statement, it is clear that it is the response from the method call that we mock.

But Consider a scenario, where the argument to the method is a pointer variable whose value is being set inside the method call. Let’s take the example of the JSON Unmarshal Method from the encoding/json package.

As mentioned in the description above, it takes a pointer as an argument and stores the result in it. So, in this case, how do we mock the response value?

The solution to this problem is in Go’s own testify package only.
testify/mock has a method named Run which can be used on the Mock Call.

Run sets a handler to be called before returning. It can be used when mocking a method (such as an Unmarshal) that takes a pointer to a struct and sets properties in such struct.

Let’s look at an example, how the Run method can be used on a Mock Call.

// Call method is used to make a Http Call and store response in 
// type pointed by v
func Call(hr HttpRequest, v interface{}) (err error)

This is the method signature that I am trying to mock, where HttpRequestis a struct in which I am passing request parameters and the result is stored in the value pointed by v.

// invoke Call method from httpClient package 
err = httpClient.Call(*httpReq, &response)

This is how the Call method is invoked, where httpReq is the struct holding the request parameters and response is the struct where the result will be stored.

Code in the image below contains the mocked method named Call. This is an auto-generated mock using mockery. This is the method which we will be using while making the mock call.

Let’s take a look at the type we want to store response in.

In the image below, you can see the following things :
1. Request variable: first argument to the Call method.
2. Response variable: second argument to Call method in which we will be storing the response of Call method.
3. Mock Call method: invoking the mock Call method and calling the Run method on mock call to set properties to a struct.

Let’s see the breakdown of the mock call in the code.

//Below, we are making a mock call to the method Call.
//We are returning nil, since i am testing a success call.
//Over the Mock call, Run method is called.
//args is the argument which we are mocking for response.
suite.httpClientService.On("Call", request, &response).Return(nil).Run(func(args mock.Arguments) {
arg := args.Get(1).(*Response)
arg.FirstName = "john"
arg.LastName = "doe"
arg.Age = 22
arg.Gender = "male"})
//In the above code, FirstName, LastName, Age, Gender are the fields //is response struct. So these will depend on the data which you //want to mock.

So, this is how we make a Mock call to the method which takes a pointer as an argument and stores response in it.

Hope this blog helped you with the problem you were facing.
Thanks for reading :D

--

--