登录
首页 >  Golang >  Go问答

返回取消引用的结构体属性而不是结构体时,单元测试会出现恐慌

来源:stackoverflow

时间:2024-02-20 08:27:54 368浏览 收藏

大家好,我们又见面了啊~本文《返回取消引用的结构体属性而不是结构体时,单元测试会出现恐慌》的内容中将会涉及到等等。如果你正在学习Golang相关知识,欢迎关注我,以后会给大家带来更多Golang相关文章,希望我们能一起进步!下面就开始本文的正式内容~

问题内容

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

我正在尝试使用 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")
        }
    })
}

解决方案


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

我个人并不认为这是错误的。我也会这样做。但经验丰富的 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,
}

这应该可以消除你的恐慌。

今天关于《返回取消引用的结构体属性而不是结构体时,单元测试会出现恐慌》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>