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

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

我可以使用嵌套接口模擬庫(kù)代碼嗎?

我可以使用嵌套接口模擬庫(kù)代碼嗎?

Go
Qyouu 2023-07-26 15:24:21
我試圖在我的 go 代碼中的測(cè)試中模擬第三方庫(kù)。但我無法編譯我所采取的方法。有什么方法可以使這項(xiàng)工作有效,或者如果我想模擬 的結(jié)果,我可以采取另一種方法T2.M2?package mainimport (    "fmt")// Two types in a library that I dont have control overtype T1 struct {}func (T1) M1() T2 {    return T2{}}type T2 struct {}func (T2) M2() {    fmt.Println("hello world")}// I created these interfaces in order to assign an instance of T1 // to a variable of type I1 so that I can mock the behavior of T2.M2()// problem is that this doesn't compile.type I1 interface {    M1() I2}type I2 interface {    M2() // I want to mock this method}// Then I would be able to create a mocktype Mock1 struct {}func (Mock1) M1() I2 {    return Mock2{}}type Mock2 struct {}func (Mock2) M2() {    fmt.Println("HELLO WORLD")}func main() {    var i1 I1    i1 = T1{}    i1.M1().M2()    i1 = Mock1{}    i1.M1().M2()}https://play.golang.org/p/sv-Uuuke1dr
查看完整描述

2 回答

?
小怪獸愛吃肉

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

將您的依賴項(xiàng)包裝在嵌入依賴類型的結(jié)構(gòu)中:


// answer wrap your dependency

type Ta1 struct {

  T1

}

func (Ta1) M1() I2 {

    return Ta2{}

}

type Ta2 struct {

   T2

}

然后它將起作用:


func main() {

    var i1 I1

    i1 = Ta1{}

    i1.M1().M2()

    i1 = Mock1{}

    i1.M1().M2()

}

嘗試一下https://play.golang.org/p/aHr78dY_c9a


查看完整回答
反對(duì) 回復(fù) 2023-07-26
?
繁華開滿天機(jī)

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

你不能在 Go 中模擬結(jié)構(gòu)。只有接口。



查看完整回答
反對(duì) 回復(fù) 2023-07-26
  • 2 回答
  • 0 關(guān)注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報(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)