agiledragon / gomonkey

gomonkey is a library to make monkey patching in unit tests easy
MIT License
2k stars 179 forks source link

apply_interface_reused_test.go 跑起来后mock失败 #36

Closed superyangege closed 3 years ago

superyangege commented 3 years ago

代码是这样的,单测失败了,应该是没mock成功,麻烦看一下哈

package main

import (
    "reflect"
    "testing"

    "git.code.oa.com/jiayanlin/go_test/fake"
    "github.com/agiledragon/gomonkey/v2"
    . "github.com/smartystreets/goconvey/convey"
)
func TestApplyInterfaceReused(t *testing.T) {
    e := &fake.Etcd{}

    Convey("TestApplyInterfaceReused", t, func() {
        patches := gomonkey.ApplyFunc(fake.NewDb, func(_ string) fake.Db {
            return e
        })
        defer patches.Reset()
        db := fake.NewDb("mysql")

        Convey("TestApplyInterface", func() {
            info := "hello interface"
            patches.ApplyMethod(reflect.TypeOf(e), "Retrieve",
                func(_ *fake.Etcd, _ string) (string, error) {
                    return info, nil
                })
            output, err := db.Retrieve("")
            So(err, ShouldEqual, nil)
            So(output, ShouldEqual, info)
        })

    })
}

跑出来后结果是这样的 * D:/go-projects/go_test/apply_interface_reused_test.go Line 30: Expected: 'hello interface' Actual: 'Hello, Mysql!' (Should be equal) goroutine 6 [running]: E:/Go/bin/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/reporting/reports.go:143 +0x74 E:/Go/bin/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/reporting/reports.go:103 +0x73