I am having trouble writing unit tests in Go for a rather common use-case/pattern.
Imagine, if you will, something like this:
package main
type Resource struct {
name string
}
type ResourceManager interface {
GetResource(id string) (*Resource, error)
GetAllResources() ([]*Resource, error)
}
type ResourceManagerImpl struct {
}
func (r *ResourceManagerImpl) GetResource(id string) (*Resource, error) {
resource := &Resource{}
var err error
// fetch resource.
// ...
return resource, err
}
func (r *ResourceManagerImpl) GetAllResources() ([]*Resource, error) {
var resources []*Resource
var err error
// for _, id := range ids {
// resource = r.GetResource(id)
// resources = append(resources, resource)
// }
return resources, err
}
It's a common pattern to have the GetAllResources
invoke GetResource
repeatedly as-needed.
I can use gomock
or testify
to test all permutations of GetResource
. But, when testing GetAllResource
, I'd like to mock GetResource
. Otherwise, testing would become sort-of-a nightmare. This is how one would do it in easymock
or mockito
in case of Java using partial mocks. But, it's not clear how the same could be achieved in Golang.
Specifically, I couldn't find how to partially mock the struct
. Most suggestions revolve around breaking such struct
s but in this case, the struct
is already at its bare minimum. It seems like a fair ask to not break the ResourceManager
interface (into single and multi) for the sake of testing as that would not make much sense and would be kludgy at best and wouldn't scale well as more such methods make into the interface.
Aucun commentaire:
Enregistrer un commentaire