运行 测试用例时模拟方法在 golang 中不起作用

Mocked method not working in golang while running the test cases

我试图在测试用例中模拟一个结构方法,但它不起作用。 我想在这里模拟 Validate 方法: `

package main

import (
    "fmt"
)

type DemoInterface interface {
    Inc(int) (int, error)
    Validate(int) error
}
type DemoStruct struct{}

func (l DemoStruct) Inc(num int) (int, error) {
    err := l.Validate(num)
    if err != nil {
        return 0, err
    }
    num = num + 100
    return num, nil

}
func (l DemoStruct) Validate(num int) error {// SOME DB LOGIC IS HERE WHICH I CAN NOT POST at Whosebug
    if num > 100 {
        return fmt.Errorf("INVALID NUM %v", num)
    }
    return nil
}

func main() {
    s, err := DemoStruct{}.Inc(10)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(s)

}

`

我的测试用例:

package main

import (
    "fmt"
    "testing"
)

const (
    SUCCESS = "SUCCESS"
    ERROR   = "ERROR"
)

type MockDemoStruct struct {
    DemoStruct
    functionality string
}

func (m MockDemoStruct) Validate(num int) error {
    switch m.functionality {
    case SUCCESS:
        return nil
    case ERROR:
        fmt.Errorf("MOCK ERROR %v", num)

    }
    return fmt.Errorf("MOCK ERROR %v", num)
}

func TestPath(t *testing.T) {

    t.Run("ERROR", func(t *testing.T) {
        ls := MockDemoStruct{DemoStruct{}, ERROR}
        res, err := ls.Inc(110)
        expected := fmt.Errorf("MOCK ERROR %v", 10)
        if err != expected {
            t.Errorf("NOT MATCH  %v  %v", err, expected)
            //NOT MATCH  INVALID NUM 110  MOCK ERROR 10

        }
        fmt.Println(res)
    })
}

这里MockDemoStruct.Validate没有被调用。 我从 Validate 得到 INVALID NUM 110,但它应该是 MOCK ERROR 110

在这种情况下,DemoStruct 中的方法 Inc 调用方法 l.Validate,其中 l 是 DemoStruct。该方法的接收者明确是 DemoStruct。所以MockDemoStruct.Validate方法不会被调用。

Go 没有您在代码中假设的继承。您不能覆盖 DemoStruct 的方法。 MockDemoStruct 组成 DemoStruct。要实际测试此方法,我建议向 DemoStruct 传递一个数据库接口,它可以在您的测试中被模拟。

我认为您还需要为 'MockDemoStruct' 实现 'Inc' 接收器,在这里您试图过度使用结构的继承 属性,看起来 GO 不支持。

为了使方法可模拟,我们将不得不使用基于 DI(依赖注入)的代码模式。

**We can mock only those methods which are injectable**.

我们有两种选择在这段代码中引入依赖注入

  1. 在接口的帮助下使用委托设计模式

  2. 使用函数作为类型引入 Monkey 补丁

使用接口委派:

type Deligation interface {
    Validate(num int) error
}

type DemoStruct struct {
    delegate Deligation
}

func (DemoStruct) Validate(num int) error {
    if num > 100 {
        return fmt.Errorf("INVALID NUM %v", num)
    }
    return nil
}
func (l DemoStruct) Inc(num int) (int, error) {
    err := l.delegate.Validate(num) // Call method using delegate
    if err != nil {
        return 0, err
    }
    num = num + 100
    return num, nil

}

func main() {
    s, err := DemoStruct{delegate: DemoStruct{}}.Inc(10) // assign delegate inside DemoStruct
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(s)

}

使用 Monkey 补丁:

func Validate(num int) error {
    if num > 100 {
        return fmt.Errorf("INVALID NUM %v", num)
    }
    return nil
}

type DemoStruct struct {
    Validate func(num int) error //  function as a type
}

func (l DemoStruct) Inc(num int) (int, error) {
    err := l.Validate(num)// It can be replaced in test cases.
    if err != nil {
        return 0, err
    }
    num = num + 100
    return num, nil

}

func main() {
    s, err := DemoStruct{Validate: Validate}.Inc(10) // assign Validate inside DemoStruct
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(s)

}

参考:https://blog.myhro.info/2018/06/how-to-mock-golang-methods