unit-testing - 如何模拟第三方包的方法

标签 unit-testing go

我有一个连接到 mongoDB 并创建新文档的简单函数。 现在如何在单元测试时模拟导入的 mongo 包的方法。

我试过通过 monkeypatching 来模拟 GinContext。

但是在导入包时无法继续模拟实际的 mongoClient。

func CreateUser(c GinContext) {
    var userdetail UserDetails
    binderr := c.ShouldBindJSON(&userdetail)
    fmt.Println(binderr)
    if binderr != nil {
        c.JSON(500, gin.H{
            "message": "Input payload not matching",
            "error":   binderr,
        })
        return
    }

    //-- Client if of type *mongo.Client. 

        //-- How do I mock the Client.Database, Client.Database.Connection

    collection := Client.Database("demo").Collection("users")
    ctx, err1 := context.WithTimeout(context.Background(), 10*time.Second)
    if err1 != nil {
    }
    response, err2 := collection.InsertOne(ctx, userdetail)
    if err2 != nil {
        log.Println("Some error inserting the document")
    }
    fmt.Println(response.InsertedID)
    c.JSON(200, gin.H{
        "message": "User created successfully",
    })
}

预期:我应该能够模拟或 stub 客户端并提供虚拟功能。就像我们在 nodeJS 中做的一样

spyOn(Client,'Database').and.return(Something)

最佳答案

每次我想知道“如何模拟一个方法”时,这主要与我的代码架构有关。无法轻松测试某些代码意味着,在大多数情况下,代码设计不当和/或与使用的库/框架过于耦合。在这里,您只想模拟 Mongo 连接,因为您的代码与 Mongo 过于紧密相关(在 CreateUser 函数中)。重构可以帮助您测试代码(无需任何 Mongo 连接)。

我体验过使用接口(interface)和依赖注入(inject)可以简化 Go 中的测试过程,并阐明了体系结构。这是我尝试帮助您测试应用程序的尝试。

代码重构

首先,定义你想用接口(interface)做什么。在这里,您要插入用户,所以让我们做一个 UserInserter 接口(interface),目前只有一个方法(Insert,插入单个用户):

type UserInserter interface {
    Insert(ctx context.Context, userDetails UserDetails) (insertedID interface{}, err error)
}

在您提供的代码中,您只使用了 insertedID,因此您可能只需要将它作为此 Insert 方法的输出(以及一个可选的错误,如果有的话出错了)。 insertedID 在这里被定义为一个接口(interface){},但您可以随意更改为您想要的任何内容。

然后,让我们修改您的CreateUser 方法并将此UserInserter 作为参数注入(inject):

func CreateUser(c *gin.Context, userInserter UserInserter) {
    var userdetail UserDetails
    binderr := c.ShouldBindJSON(&userdetail)
    fmt.Println(binderr)
    if binderr != nil {
        c.JSON(500, gin.H{
            "message": "Input payload not matching",
            "error":   binderr,
        })
        return
    }

    // this is the modified part
    insertedID, err2 := userInserter.Insert(c, userdetail)
    if err2 != nil {
        log.Println("Some error inserting the document")
    }
    fmt.Println(insertedID)

    c.JSON(200, gin.H{
        "message": fmt.Sprintf("User %s created successfully", insertedID),
    })
}

这个方法可以重构,但是为了避免混淆,我不会碰它。

userInserter.Insert(c, userdetail) 通过注入(inject) userInserter 在此方法中替换 Mongo 依赖。

您现在可以使用您选择的后端(在您的情况下为 Mongo)实现您的 UserInserter 接口(interface)。插入 Mongo 需要一个 Collection 对象(我们要将用户插入其中的集合),所以让我们将其添加为一个属性:

type MongoUserInserter struct {
    collection *mongo.Collection
}

Insert 方法的实现如下(在 *mongo.Collection 上调用 InsertOne 方法):

func (i MongoUserInserter) Insert(ctx context.Context, userDetails UserDetails) (insertedID interface{}, err error) {
    response, err := i.collection.InsertOne(ctx, userDetails)
    return response.InsertedID, err
}

这个实现可以在一个单独的包中,应该单独测试。

实现后,您可以在主应用程序中使用 MongoUserInserter,其中 Mongo 是后端。 MongoUserInserter在main函数中初始化,在CreateUser方法中注入(inject)。路由器设置已分开(也用于测试目的):

func setupRouter(userInserter UserInserter) *gin.Engine {
    router := gin.Default()

    router.POST("/createUser", func(c *gin.Context) {
        CreateUser(c, userInserter)
    })

    return router
}

func main() {
    client, _ := mongo.NewClient()
    collection := client.Database("demo").Collection("users")
    userInserter := MongoUserInserter{collection: collection}

    router := setupRouter(userInserter)
    router.Run(":8080")
}

请注意,如果有一天你想更改后端,你只会 需要修改main函数中的userInserter!

测试

从测试的角度来看,现在更容易测试,因为我们可以创建一个假的 UserInserter,例如:

type FakeUserInserter struct{}

func (_ FakeUserInserter) Insert(ctx context.Context, userDetails UserDetails) (insertedID interface{}, err error) {
    return userDetails.Name, nil
}

(我假设这里 UserDetails 有一个属性 Name)。

如果你真的想模拟这个接口(interface),你可以看看GoMock .不过在这种情况下,我不确定是否需要使用模拟框架。

现在我们可以使用简单的 HTTP 测试框架(参见 https://github.com/gin-gonic/gin#testing)测试我们的 CreateUser 方法,而无需 Mongo 连接或模拟它。

import (
    "bytes"
    "net/http"
    "net/http/httptest"
    "testing"

    "github.com/stretchr/testify/assert"
)

func TestCreateUser(t *testing.T) {
    userInserter := FakeUserInserter{}
    router := setupRouter(userInserter)

    w := httptest.NewRecorder()
    body := []byte(`{"name": "toto"}`)
    req, _ := http.NewRequest("POST", "/createUser", bytes.NewBuffer(body))
    router.ServeHTTP(w, req)

    assert.Equal(t, 200, w.Code)
    assert.Equal(t, `{"message":"User toto created successfully"}`, w.Body.String())
}

请注意,这并不免除同时测试 MongoUserInserterInsert 方法,而是单独测试:这里,此测试涵盖 CreateUser,而不是插入方法。

关于unit-testing - 如何模拟第三方包的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56391444/

相关文章:

c# - 如何从虚假网站获取 Sitecore 项目

spring - 如何使用 Spring 4 和注释编写单元测试来验证异步行为?

visual-studio - 在 VS2015 的调试中运行单元测试时出现 DisconnectedContext 错误

java - 模拟方法未返回预期结果

go - 如何去 :generate stringer constants from multiple packages?

go - time.Millisecond * int 混淆

teamcity 中的 python nose 测试报告

string - 为 unicode 字母写一个 toUpper 函数

sql - Gorm Ping Db碎片

go - 在 goroutine 中使用指针接收器