I'm having difficulty writing test for the case below.
I'm able to write test for "helper" with a mock object that only implements the functions that used by myself.
How do I write test code for the function 'new' using a mock object without mocking functions C(), D()?
It could be that the other package is written poorly that it should not return an Interface but rather the actual struct?
package main
import (
"fmt"
)
func main() {
New()
}
func New() {
new(NewFromEnvironment)
}
type newTopology func()(Interface,error)
// new is non-exposed simply used for testing purpose.
func new(newTopology newTopology) {
t,_ := newTopology()
helper(t)
}
// I need to call only A and B
type topologyInterface interface {
A() string
B() string
}
func helper(topology topologyInterface) {
s1 := topology.A()
s2 := topology.B()
fmt.Println(s1 + "," + s2)
}
// Below are from other package named "topology".
// I have no control to the code below.
type Interface interface {
A() string
B() string
C() string
D() string
//... more
}
func NewFromEnvironment() (Interface, error) {
return P{}, nil
}
type P struct{}
func (p P) A() string {
return "A"
}
func (p P) B() string {
return "B"
}
func (p P) C() string {
return "C"
}
func (p P) D() string {
return "D"
}
// more...