第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

運(yùn)行測(cè)試用例時(shí),模擬方法在 golang 中不起作用

運(yùn)行測(cè)試用例時(shí),模擬方法在 golang 中不起作用

Go
搖曳的薔薇 2022-05-05 15:57:12
我試圖在測(cè)試用例中模擬一個(gè)結(jié)構(gòu)方法,但它不起作用。我想在這里模擬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)}`我的測(cè)試用例: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)    })}這里沒有調(diào)用MockDemoStruct.Validate 。我從 Validate 得到INVALID NUM 110,但它應(yīng)該是MOCK ERROR 110
查看完整描述

3 回答

?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊

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

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


查看完整回答
反對(duì) 回復(fù) 2022-05-05
?
智慧大石

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊

為了使該方法可模擬,我們將不得不使用基于 DI(依賴注入)的代碼模式。

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

我們有兩個(gè)選項(xiàng)可以在此代碼中引入依賴注入。

  1. 在界面的幫助下使用委托設(shè)計(jì)模式

  2. 使用函數(shù)作為類型引入 Monkey 修補(bǔ)

使用接口委托:

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)


}

使用猴子補(bǔ)丁:


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)


}


查看完整回答
反對(duì) 回復(fù) 2022-05-05
?
一只斗牛犬

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊

我認(rèn)為你還需要為'MockDemoStruct'實(shí)現(xiàn)'Inc'接收器,在這里你試圖過(guò)度使用結(jié)構(gòu)的繼承屬性,看起來(lái)GO不支持。



查看完整回答
反對(duì) 回復(fù) 2022-05-05
  • 3 回答
  • 0 關(guān)注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)