4

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

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

我想为 编写单元测试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)
    }
}

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

4

2 回答 2

0

使用猴子库挂钩来自 mongo 驱动程序的任何函数。

例如:

func insert(collection *mongo.Collection) (int, error) {
    ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)

    u := User{
        Name: "kevin",
        Age:  20,
    }

    res, err := collection.InsertOne(ctx, u)
    if err != nil {
        log.Printf("error: %v", err)
        return 0, err
    }

    id := res.InsertedID.(int)
    return id, nil
}

func TestInsert(t *testing.T) {
    var c *mongo.Collection

    var guard *monkey.PatchGuard
    guard = monkey.PatchInstanceMethod(reflect.TypeOf(c), "InsertOne",
        func(c *mongo.Collection, ctx context.Context, document interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) {
            guard.Unpatch()
            defer guard.Restore()

            log.Printf("record: %+v, collection: %s, database: %s", document, c.Name(), c.Database().Name())
            res := &mongo.InsertOneResult{
                InsertedID: 100,
            }

            return res, nil
        })

    collection := client.Database("db").Collection("person")
    id, err := insert(collection)

    require.NoError(t, err)
    assert.Equal(t, id, 100)
}
于 2019-07-10T02:52:55.613 回答
0

编写可测试的最佳解决方案可能是将代码提取到 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
}

当你需要模拟它时,只需创建一个 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")
}

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

于 2019-06-26T13:18:11.620 回答