mardi 24 mars 2015

Mocking receiver functions in Go

I'm trying to unit test a receiver function that calls other receiver functions in that struct.


Let's say I want to test Three() and mock the call to two() in the following:



type MyStruct struct {
a string
b string
}

func (m *MyStruct) one() int {
return 2
}

func (m *MyStruct) two() int {
return m.one() * 2
}

func (m *MyStruct) Three() int {
return m.two() * 2
}


I was following method two of the following answer.


I created a custom constructor for every single function that I wanted to unit test and overrode those methods with mocked versions. But I thought it may not be easy to maintain the code once the number of functions grow.


Is there any preferred way of mocking such functions? I wish the official documentation had some guidelines on how to mock things in different scenarios, similar to what mox on Python provides.


Also, note that I don't want to use a third party mocking library.


Aucun commentaire:

Enregistrer un commentaire