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

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

你如何模擬 *ec2.DescribeVolumesOutput 的值

你如何模擬 *ec2.DescribeVolumesOutput 的值

Go
元芳怎么了 2023-06-19 17:13:11
我目前正在嘗試適應 TDD,并且在我正在進行的當前項目中,我正在嘗試利用 AWS 的 Go SDK。這一切都很好,花花公子,我以前用過它,但我目前正在嘗試模擬發(fā)送的值*ec2.DescribeVolumesOutput。深入研究代碼,我將其視為返回的內容*ec2.DescribeVolumesOutput:type DescribeVolumesOutput struct {    _ struct{} `type:"structure"`    // The NextToken value to include in a future DescribeVolumes request. When    // the results of a DescribeVolumes request exceed MaxResults, this value can    // be used to retrieve the next page of results. This value is null when there    // are no more results to return.    NextToken *string `locationName:"nextToken" type:"string"`    // Information about the volumes.    Volumes []*Volume `locationName:"volumeSet" locationNameList:"item" type:"list"`}好的..這很酷,但我想要模擬的輸出必須位于Volumes []*VolumelocationName:"volumeSet" locationNameList:"item" type:"list"` 中所以讓我們更深入一點,看看那是什么......好的!這看起來像是我想模擬其值的一些數(shù)據(jù)!但在過去的幾天里,我并沒有真正嘲笑這些價值觀。它們是否如此嵌套以至于這種類型的嘲笑不值得付出努力?即使嘗試使用 似乎也github.com/aws/aws-sdk-go/service/ec2/ec2iface無法幫助我思考如何正確打包一些模擬值返回以進行測試。我來參加 TDD 是不是全錯了?我錯過了一些非常明顯的東西嗎?我真的沒有示例代碼可以展示,因為我現(xiàn)在不再理解我想要做什么。有沒有人可能有他們如何嘲笑這個的例子?
查看完整描述

1 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

無法模擬類型,您只能模擬接口的實現(xiàn)。在您的情況下,我假設您正在嘗試調用DescribeVolumes并在響應中獲取由您構造的值。
為此,您需要創(chuàng)建一個模擬

type MockEC2API struct {

? ? ec2iface.EC2API // embedding of the interface is needed to skip implementation of all methods

? ? DescribeVolumesMethod func(*ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error)

}


func (m *MockEC2API) DescribeVolumes(in *ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error) {

? ? if m.DescribeVolumesMethod != nil {

? ? ? ? return m.DescribeVolumesMethod(in)

? ? }

? ? return nil, nil // return any value you think is good for you

}

在測試中創(chuàng)建實例MockEC2API而不是真實實例ec2.EC2并為其提供將被調用的函數(shù)并返回您準備好的ec2.DescribeVolumesOutput結果


...

ec2 := &MockEC2API{

? ? DescribeVolumesMethod: func(*ec2.DescribeVolumesInput) (*ec2.DescribeVolumesOutput, error) {

? ? ? ? return &ec2.DescribeVolumesOutput{...your initialization...}, nil

? ? }

}

...

output, err := ec2.DescribeVolumes(in) // this output will be your prepared initialization



查看完整回答
反對 回復 2023-06-19
  • 1 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號