dqfaom241360 2018-11-01 09:22
浏览 23
已采纳

在Golang中模拟内部功能

I want to mock function using interface and I was able to mock the first function callsomething With great help from icza , Now it’s a bit more tricky . I want to test function vl1 With mock for function function1 , how it can be done.

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

package main

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)
            }
        })
    }
}
  • 写回答

1条回答 默认 最新

  • doucang6914 2018-11-01 13:43
    关注

    If you want to mock a function in Go you'll have to declare a variable that will hold the function value, have all callers of the function use the variable instead and then during tests you can switch up the function value for a mock implementation.

    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
        }()
        // ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证