亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

運行測試用例時,模擬方法在 golang 中不起作用

運行測試用例時,模擬方法在 golang 中不起作用

Go
搖曳的薔薇 2022-05-05 15:57:12
我試圖在測試用例中模擬一個結構方法,但它不起作用。我想在這里模擬Validate方法:`package mainimport (    "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 Stackoverflow    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 mainimport (    "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
查看完整描述

3 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

在這種情況下,Inc中的方法DemoStruct調用l.Validatel 是 a的方法DemoStruct。該方法的接收者明確地是一個DemoStruct. 所以MockDemoStruct.Validate不會調用該方法。

Go 沒有您在代碼中假設的繼承。您不能覆蓋DemoStructMockDemoStruct組成DemoStruct. _ 為了實際測試這個方法,我建議傳遞DemoStruct一個 db 接口,它可以在你的測試中被模擬。


查看完整回答
反對 回復 2022-05-05
?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

為了使該方法可模擬,我們將不得不使用基于 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)


}

使用猴子補?。?/p>


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)


}


查看完整回答
反對 回復 2022-05-05
?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

我認為你還需要為'MockDemoStruct'實現'Inc'接收器,在這里你試圖過度使用結構的繼承屬性,看起來GO不支持。



查看完整回答
反對 回復 2022-05-05
  • 3 回答
  • 0 關注
  • 166 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號