在 Go 中模拟 MongoDB 响应

7 go mongodb mongo-go

我正在从 MongoDB 获取文档并将其传递给函数transform,例如

var doc map[string]interface{}
err := collection.FindOne(context.TODO(), filter).Decode(&doc) 
result := transform(doc)
Run Code Online (Sandbox Code Playgroud)

我想为 编写单元测试transform,但我不确定如何模拟来自 MongoDB 的响应。理想情况下我想设置这样的东西:

func TestTransform(t *testing.T) {
    byt := []byte(`
    {"hello": "world",
     "message": "apple"}
 `)

    var doc map[string]interface{}

    >>> Some method here to Decode byt into doc like the code above <<<

    out := transform(doc)
    expected := ...
    if diff := deep.Equal(expected, out); diff != nil {
        t.Error(diff)
    }
}
Run Code Online (Sandbox Code Playgroud)

一种方法是json.Unmarshalinto doc,但这有时会产生不同的结果。例如,如果 MongoDB 中的文档中有一个数组,那么该数组将被解码为doc类型bson.A而不是[]interface{}类型。

Ang*_*ala 5

我团队的一名成员最近发现 GO 官方 MongoDB 驱动程序中有一个隐藏的宝石:https://pkg.go.dev/go.mongodb.org/mongo-driver@v1.9.1/mongo/integration/mtest。尽管该包处于实验模式并且不保证向后兼容性,但它可以帮助您执行单元测试,至少使用此版本的驱动程序。

您可以查看这篇很酷的文章,其中有大量有关如何使用它的示例:https://medium.com/@victor.neuret/mocking-the-official-mongo-golang-driver-5aad5b226a78。此外,这里是包含本文代码示例的存储库: https: //github.com/victorneuret/mongo-go-driver-mock

因此,根据您的示例和文章中的示例,我认为您可以尝试类似以下的操作(当然,您可能需要对此进行调整和实验):

func TestTransform(t *testing.T) {
    mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
    defer mt.Close()

    mt.Run("find & transform", func(mt *mtest.T) {
        myollection = mt.Coll
        expected := myStructure{...}

        mt.AddMockResponses(mtest.CreateCursorResponse(1, "foo.bar", mtest.FirstBatch, bson.D{
            {"_id", expected.ID},
            {"field-1", expected.Field1},
            {"field-2", expected.Field2},
        }))

        response, err := myFindFunction(expected.ID)
        if err != nil {
            t.Error(err)
        }

        out := transform(response)
        if diff := deep.Equal(expected, out); diff != nil {
            t.Error(diff)
        }
    })
}

Run Code Online (Sandbox Code Playgroud)

或者,您可以通过与 Docker 容器的集成测试以自动化的方式执行更真实的测试。有一些很好的软件包可以帮助您解决此问题:

我遵循这种方法与dockertest库来自动化完整的集成测试环境,可以setUp通过tearDown命令go test -v -run Integration。请参阅此处的完整示例: https: //github.com/AnhellO/learn-dockertest/tree/master/mongo

希望这可以帮助。


ale*_*r96 1

编写可测试的最佳解决方案可能是将代码提取到 DAO 或数据存储库。您将定义一个接口来返回您需要的内容。这样,您就可以使用模拟版本进行测试。

// repository.go
type ISomeRepository interface {
    Get(string) (*SomeModel, error)
}

type SomeRepository struct { ... }

func (r *SomeRepository) Get(id string) (*SomeModel, error) {
    // Handling a real repository access and returning your Object
}
Run Code Online (Sandbox Code Playgroud)

当你需要模拟它时,只需创建一个 Mock-Struct 并实现接口:

// repository_test.go

type SomeMockRepository struct { ... }

func (r *SomeRepository) Get(id string) (*SomeModel, error) {
    return &SomeModel{...}, nil
}

func TestSomething() {
    // You can use your mock as ISomeRepository
    var repo *ISomeRepository
    repo = &SomeMockRepository{}
    someModel, err := repo.Get("123")
}
Run Code Online (Sandbox Code Playgroud)

这最好与某种依赖注入一起使用,因此将此存储库作为 ISomeRepository 传递到函数中。