go - 在 golang 中模拟内部函数

标签 go dependency-injection interface

我想使用接口(interface)模拟函数,我能够模拟第一个函数 callsomethingicza 的大力帮助下,现在有点棘手了。我想测试函数 vl1 使用 mock for function function1 ,它是如何完成的。

https://play.golang.org/p/w367IOjADFV

主要包

import (
    "fmt"
    "time"
    "testing"
)

type vInterface interface {
    function1() bool
}

type mStruct struct {
    info string
    time time.Time
}

func (s *mStruct) function1() bool {
    return true
}

func callSomething(si vInterface) bool {
    return si.function1()
}

func (s *mStruct) vl1() bool {
    return callSomething(s)
}

var currentVt1 mStruct

func main() {
    vl1 := currentVt1.vl1()

    fmt.Println(vl1)
}

//——————————————————TESTS——————————————————

// This test is working as expected (as suggested by icza) for the function  "callSomething"
// here we use mock interface and mock function which helps to mock function1
type mockedVInterface struct {
    value bool
}

func (m mockedVInterface) fn1() bool {
    return m.value
}

func Test_callSomething(t *testing.T) {



    type args struct {
        si vInterface
    }
    tests := []struct {
        name string
        args args
        want bool
    }{
        {
            name: "first value",
            args: args{
                si: mockedVInterface{value: false},
            },
            want: false,
        },
        {
            name: "second value",
            args: args{
                si: mockedVInterface{value: true},
            },
            want: true,
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := callSomething(tt.args.si); got != tt.want {
                t.Errorf("callSomething() = %v, want %v", got, tt.want)
            }
        })
    }
}



//----Here is the test which a bit tricky 
//I want to call to "s.vl1()" and test it with mock for "function1"

func Test_mStruct_vl1(t *testing.T) {
    type fields struct {
        info string
        time time.Time
    }
    tests := []struct {
        name   string
        fields fields
        want   bool
    }{
        {
            name: "test 2",
            fields: struct {
                info string
                time time.Time
            }{info: "myinfo", time: time.Now() },

        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            s := &mStruct{
                info: tt.fields.info,
                time: tt.fields.time,
            }
            //here the test is starting
            if got := s.vl1(); got != tt.want {
                t.Errorf("mStruct.vl1() = %v, want %v", got, tt.want)
            }
        })
    }
}

最佳答案

如果你想在 Go 中模拟一个函数,你必须声明一个变量来保存函数值,让函数的所有调用者都使用该变量,然后在测试期间你可以将函数值切换为模拟实现。

func callSomethingFunc(si vInterface) bool {
    return si.function1()
}

var callSomething = callSomethingFunc

func (s *mStruct) vl1() bool {
    return callSomething(s)
}

// ...

func Test_mStruct_vl1(t *testing.T) {
    callSomething = func(si vInterface) bool { // set mock
        // do mock stuff
    }
    defer func(){
        callSomething = callSomethingFunc // set back original func at end of test
    }()
    // ...

关于go - 在 golang 中模拟内部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53098327/

相关文章:

go - 从 terraform 模式访问 Golang 中的排版并遍历 Map

java - 从一个 ORM 迁移到另一个

spring - Mockito @InjectMocks 如何工作?

android - Dagger 2中 "HasFragmentInjector"的实际用法是什么

go - 与返回自身的方法接口(interface)

java - 实例化接口(interface)后无需编写任何代码即可使用接口(interface)的方法

go - 循环映射时去追加很奇怪

string - 子字符串和 Go 垃圾收集器

java - 如何在 spring 中为运行时动态创建的对象注入(inject)依赖?

c# - 为什么 DateTime.ToDateTime( ) 不能编译?