I'm writing unit tests for a function that I developed in golang. Here is the code snippet:
func myFunc () {
//there is some code
err := json.Unmarshal (a, &b)
if err != nil {
//handle error
}
//there is some more code
err := json.Unmarshal (c, &d)
if err != nil {
//handle error
}
}
Now I want to mock first unmarshal to return success and second unmarshal to return failure. In python, I see one similar post: Python mock multiple return values
But I cannot find one for golang. Can anyone please help me on this.
What I would do in this situation is have an exported package variable Unmarshaller
and set it to json.Unmarshal
in the init()
method.
var Unmarshaller func(data []byte, v interface{}) error;
func init() {
Unmarshaller = json.Unmarshal
}
Then, when you want to force an error, you can just do
mypackage.Unmarshaller = func(data[] byte, v interface{}) error {
return errors.New("It broke!")
}
Then, in your code, instead of calling json.Unmarshal
directly, you call your package level `Unmarshaller, so
err =: json.Unmarshal(jsonBytes, &test)
would become
err =: Unmarshaller(jsonBytes, &test)
As a short example: https://play.golang.org/p/CinLmprtp5