【问题标题】:Unit tests panic when returning dereferenced struct attribute rather than struct返回取消引用的结构属性而不是结构时,单元测试会出现恐慌
【发布时间】:2021-04-22 08:34:08
【问题描述】:

如何测试返回字符串或数字类型的结构属性而不是结构本身的函数?

我正在尝试使用Test Code 块测试Lambda Code 块。

在下面的 Lambda Code 块中,我返回 *resp.UserPoolClient.ClientSecret 取消引用 string,而不是 *string

当我运行我的测试时,我相信我会收到一个恐慌错误,因为在调试器中 *resp.UserPoolClient.ClientSecretnil

我返回取消引用的属性是错误的方法吗?想知道我是否最好只返回整个 resp 对象,而不是取消引用?我这样做是因为我根本不需要修改值,只需要可供参考的副本。

Lambda 代码

package main
 
import (
    "fmt"
    "log"
    "os"
 
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/awserr"
    "github.com/aws/aws-lambda-go/lambda"
    "github.com/aws/aws-sdk-go/aws/session"
    cidp "github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
    cidpif "github.com/aws/aws-sdk-go/service/cognitoidentityprovider/cognitoidentityprovideriface"
    util "github.com/sean/repo/internal/util"
)
 
type application struct {
    config configuration
}
 
type configuration struct {
    ClientPoolID string
    UserPoolID   string
    idp          cidpif.CognitoIdentityProviderAPI
}
 
func (app application) getUserPoolClientSecret() (string, error) {
    input := &cidp.DescribeUserPoolClientInput{
        UserPoolId: aws.String(app.config.UserPoolID),
        ClientId:   aws.String(app.config.ClientPoolID),
    }
 
    resp, err := app.config.idp.DescribeUserPoolClient(input)
    if err != nil {
        if aerr, ok := err.(awserr.Error); ok {
            log.Printf("[ERROR] %v", aerr.Error())
        } else {
            log.Printf("[ERROR] %v", err.Error())
        }
        return "", err
    }
    log.Println("[INFO] Obtained user pool client secret successfully")
    return *resp.UserPoolClient.ClientSecret, nil
}

// omitted for brevity
 
func main() {
    config := configuration{
        ClientPoolID: os.Getenv("CLIENT_POOL_ID"),
        UserPoolID:   os.Getenv("USER_POOL_ID"),
        idp:          cidp.New(session.Must(session.NewSession())),
    }
 
    app := application{config: config}
 
    lambda.Start(app.handler) // handler() calls app.getUserPoolClientSecret
}

测试代码

package main
 
import (
    "testing"
 
    cidp "github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
    cidpif "github.com/aws/aws-sdk-go/service/cognitoidentityprovider/cognitoidentityprovideriface"
)
 
 
type mockDescribeUserPoolClient struct {
    cidpif.CognitoIdentityProviderAPI
    Response *cidp.DescribeUserPoolClientOutput
    Error    error
}
 
func (m mockDescribeUserPoolClient) DescribeUserPoolClient(*cidp.DescribeUserPoolClientInput) (*cidp.DescribeUserPoolClientOutput, error) {
    return m.Response, nil
}
 
func TestGetUserPoolClientSecret(t *testing.T) {
    t.Run("Successfully obtained client pool secret", func(t *testing.T) {
        idpMock := mockDescribeUserPoolClient{
            Response: &cidp.DescribeUserPoolClientOutput{},
            Error:    nil,
        }
 
        app := application{config: configuration{
            ClientPoolID: "test",
            UserPoolID:   "test",
            idp:          idpMock,
        }}
 
        _, err := app.getUserPoolClientSecret()
        if err != nil {
            t.Fatal("App secret should have been obtained")
        }
    })
}

【问题讨论】:

    标签: amazon-web-services unit-testing go aws-lambda


    【解决方案1】:

    我返回取消引用的属性是错误的方法吗?

    我个人认为没有错。我也会这样做。但更有经验的 Go 开发者可能会在这里提供更细致入微和详细的答案。

    关于恐慌,我认为问题在于您创建的模拟没有返回所有必需的信息。

    你的模拟:

    idpMock := mockDescribeUserPoolClient{
        Response: &cidp.DescribeUserPoolClientOutput{},
        Error:    nil,
    }
    

    您只创建DescribeUserPoolClientOutput 的“空”实例。但是您要测试的代码确实访问了您未定义的两个孩子:

    1. UserPoolClient(结构参考)
    2. ClientSecret(字符串引用)

    您的代码:

    *resp.UserPoolClient.ClientSecret
    

    所以你的模拟也需要模拟那些:

    idpMock := mockDescribeUserPoolClient{
        Response: &cidp.DescribeUserPoolClientOutput{
            UserPoolClient: &cidp.UserPoolClientType{
                ClientSecret: aws.String("example-secret")
            }
        },
        Error:    nil,
    }
    

    这应该可以解决你的恐慌。

    【讨论】:

    • 模拟响应非常有意义。模拟响应也使得返回取消引用的字符串完全没问题 IMO。这非常有帮助,非常感谢!
    猜你喜欢
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 2011-03-04
    • 2022-11-08
    • 1970-01-01
    相关资源
    最近更新 更多