I'm trying to build mock class for unit test in golang; does anyone know how to do that? For example, in the following code slice, I'd like to print FakeMyClass.Object's return value.
package main
import (
"fmt"
)
type MyClass struct {
}
func (*MyClass) Object() (int) {
return 0
}
func (mc *MyClass) PrintInfo() {
fmt.Printf("%v
", mc.Object())
}
type FakeMyClass struct {
MyClass
}
func (*FakeMyClass) Object() (int) {
return 1
}
func main() {
mc := &FakeMyClass{}
mc.PrintInfo()
}
I prefer to use github.com/stretchr/testify/tree/master/mock
package main
import (
"fmt"
"github.com/stretchr/testify/mock"
)
type MyClass struct {
}
func (*MyClass) Object() (int) {
return 0
}
func (mc *MyClass) PrintInfo() {
fmt.Printf("%v
", mc.Object())
}
type FakeMyClass struct {
mock.Mock
}
func (fmc *FakeMyClass) Object() (int) {
args := fmc.Called()
return args.Int(0)
}
func (fmc *FakeMyClass) PrintInfo() {
fmt.Printf("%v
", fmc.Object())
}
func main() {
mc := &FakeMyClass{}
mc.On("Object").Return(1)
mc.PrintInfo()
//mc.AssertExpectations(t)
}
You can make a struct that returns mocked functions, which you pass in with each test case for values you want returned. For example:
package main
import (
"fmt"
)
type MyClass struct{}
func (*MyClass) Object() int {
return 0
}
func (mc *MyClass) PrintInfo() {
fmt.Printf("%v
", mc.Object())
}
type FakeMyClass struct {
ObjectMock: func() int
}
func (fm *FakeMyClass) Object() int {
return fm.ObjectMock()
}
func (fm *FakeMyClass) PrintInfo() {
fmt.Printf("%v
", fm.Object())
}
func main() {
mc := &FakeMyClass{
// Create the mocked function here, to be called when the Object() function is called.
ObjectMock: func() int {
return 0
}
}
mc.PrintInfo()
}
You are looking for virtual dispatch on the Object()
method. The only form of virtual dispatch that I know of in Go is to use interfaces.
PrintInfo
will then be a function that uses that interface type.
E.g.:
package main
import (
"fmt"
)
type WithObject interface {
Object() int
}
func PrintInfo(wo WithObject) {
fmt.Printf("%v
", wo.Object())
}
type MyClass struct {
}
func (*MyClass) Object() (int) {
return 0
}
type FakeMyClass struct {
MyClass
}
func (*FakeMyClass) Object() (int) {
return 1
}
func main() {
fmc := &FakeMyClass{}
PrintInfo(fmc)
}
I used mockgen for it:
mockgen -source myModule.go -package myPackage -destination myModuleMock.go
You can install it by: got get github.com/golang/mock