lundi 20 août 2018

How to test unexported struct field in a different package in go?

I have a package that creates objects with a factory. The structs have unexported fields, e.g.:

package fetcher
type GitFetcher struct {
    uri    string
}

I have another package that parses some config files and then builds another object which uses the above objects:

package config
type Source struct {
    fetcher    GitFetcher
}

I'm trying to test my config package. I want to build some expected objects, but since my tests are in config and my GitFetcher is in fetcher I can't just create the objects I want, e.g.:

package config
expected := GitFetcher{
    uri: "example.com/repo.git"      // doesn't work. Field isn't exported.
}

How can I build objects across packages for testing like this? I don't want to use the fetcher factory method since the parameters it takes aren't straightforward.

Aucun commentaire:

Enregistrer un commentaire