英文:
How to build mock class in golang?
问题
我正在尝试在Golang中为单元测试构建模拟类,有人知道如何做吗?例如,在下面的代码片段中,我想打印出FakeMyClass.Object的返回值。
package main
import (
"fmt"
)
type MyClass struct {
}
func (*MyClass) Object() int {
return 0
}
func (mc *MyClass) PrintInfo() {
fmt.Printf("%v\n", mc.Object())
}
type FakeMyClass struct {
MyClass
}
func (*FakeMyClass) Object() int {
return 1
}
func main() {
mc := &FakeMyClass{}
mc.PrintInfo()
}
英文:
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\n", mc.Object())
}
type FakeMyClass struct {
MyClass
}
func (*FakeMyClass) Object() (int) {
return 1
}
func main() {
mc := &FakeMyClass{}
mc.PrintInfo()
}
答案1
得分: 2
我更喜欢使用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\n", 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\n", fmc.Object())
}
func main() {
mc := &FakeMyClass{}
mc.On("Object").Return(1)
mc.PrintInfo()
//mc.AssertExpectations(t)
}
英文:
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\n", 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\n", fmc.Object())
}
func main() {
mc := &FakeMyClass{}
mc.On("Object").Return(1)
mc.PrintInfo()
//mc.AssertExpectations(t)
}
答案2
得分: 1
我用mockgen来实现:
mockgen -source myModule.go -package myPackage -destination myModuleMock.go
你可以通过以下方式安装它:
go get github.com/golang/mock
英文:
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
答案3
得分: 0
你可以创建一个返回模拟函数的结构体,在每个测试用例中传入你想要返回的值。例如:
package main
import (
"fmt"
)
type MyClass struct{}
func (*MyClass) Object() int {
return 0
}
func (mc *MyClass) PrintInfo() {
fmt.Printf("%v\n", mc.Object())
}
type FakeMyClass struct {
ObjectMock func() int
}
func (fm *FakeMyClass) Object() int {
return fm.ObjectMock()
}
func (fm *FakeMyClass) PrintInfo() {
fmt.Printf("%v\n", fm.Object())
}
func main() {
mc := &FakeMyClass{
// 在这里创建模拟函数,在调用 Object() 函数时将被调用。
ObjectMock: func() int {
return 0
},
}
mc.PrintInfo()
}
这段代码展示了如何创建一个模拟函数的结构体,并在调用 Object()
函数时使用模拟函数返回值。
英文:
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\n", mc.Object())
}
type FakeMyClass struct {
ObjectMock: func() int
}
func (fm *FakeMyClass) Object() int {
return fm.ObjectMock()
}
func (fm *FakeMyClass) PrintInfo() {
fmt.Printf("%v\n", 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()
}
答案4
得分: 0
你正在寻找在Object()
方法上进行虚拟调度。我所知道的Go语言中唯一的虚拟调度形式是使用接口。
PrintInfo
将成为一个使用该接口类型的函数。
例如:
package main
import (
"fmt"
)
type WithObject interface {
Object() int
}
func PrintInfo(wo WithObject) {
fmt.Printf("%v\n", 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)
}
希望这能帮到你!
英文:
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\n", 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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论