我正在使用两个AWS服务(Glue和DynamoDB),这两个服务都具有相同的方法名称CreateTable,因此在模拟这些服务时,出现了CreateTable is ambiguous错误。

胶水:CreateTable https://docs.aws.amazon.com/sdk-for-go/api/service/glue/#Glue.CreateTable

DynamoDB:CreateTable https://docs.aws.amazon.com/sdk-for-go/api/service/dynamodb/#DynamoDB.CreateTable

有什么办法解决这个问题?

码:

type UpdateWorkflow struct {
    glueIface glueiface.GlueAPI
    dbIface   dynamodbiface.DynamoDBAPI
    tableName string
}

func NewUpdateWorkflow(tableName string) *UpdateWorkflow {
    sess := sessions.NewSession()
    return &UpdateWorkflow{
        dbIface:   dynamodb.New(sess),
        glueIface: glue.New(sess),
        tableName: tableName,
    }
}

单元测试:
// MockUpdateWorkflow is a mock implementation of gluetestutils and dynamodb service
type MockUpdateWorkflow struct {
    glueiface.GlueAPI
    dynamodbiface.DynamoDBAPI
    mock.Mock
}

func setup() (*UpdateWorkflow, *MockUpdateWorkflow) {
    mockClient := new(MockUpdateWorkflow)
    mockServices := &UpdateWorkflow{
        glueIface: mockClient,
        dbIface:   mockClient,
        tableName: mockTableName,
    }
    return mockServices, mockClient
}

最佳答案

如果存在冲突的函数名称,则无法嵌入接口,必须使用命名字段:

type MockUpdateWorkflow struct {
    g glueiface.GlueAPI
    d dynamodbiface.DynamoDBAPI
    mock.Mock
}

func setup() (*UpdateWorkflow, *MockUpdateWorkflow) {
    mockClient := new(MockUpdateWorkflow)
    mockServices := &UpdateWorkflow{
        glueIface: mockClient.g,
        dbIface:   mockClient.d,
        tableName: mockTableName,
    }
    return mockServices, mockClient
}

关于amazon-web-services - Golang:模拟具有相同方法名称的AWS服务,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59035071/

10-11 07:25