Go Stubs: Interface
Interfaces for testing in Go. Ways to create stubs, without having to have a lot of exported interfaces in the application code. Instead, the developer using the package can create the necessary stubs.
References:
package main
import "fmt"
type acknowledger interface {
Ack(sub string, id ...string) error
}
func Ack(sub string, id ...string) error {
fmt.Printf("sub: %s\n",sub)
for _,v := range id {
fmt.Printf("i %s\n",v)
}
return nil
}
type P struct {}
func (p P) Ack(sub string, id ...string) error {
fmt.Printf("sub: %s\n",sub)
for _,v := range id {
fmt.Printf("i %s\n",v)
}
return nil
}
func A(a acknowledger) {
a.Ack("one","a","b")
}
func myNew() acknowledger {
p := P{}
return p
}
var acker acknowledger = myNew()
type mockClient struct{
count int
}
func (c *mockClient) Ack(sub string, id ...string) error {
c.count += 1
return nil
}
func (c *mockClient) Count() int {
return c.count
}
func main() {
acker.Ack("a")
c := mockClient{}
acker = &c
A(acker)
A(acker)
fmt.Printf(" %d\n",c.count)
}
This thought needs further developmentā¦