Mongoose 聚合查询在 Jest/Mockgoose 测试中失败,可以在其他地方使用 [英] Mongoose aggregation query fails in Jest/Mockgoose test, works elsewhere

查看:23
本文介绍了Mongoose 聚合查询在 Jest/Mockgoose 测试中失败,可以在其他地方使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的 Mongoose 模型创建一些测试,但我不知道如何让 Jest/Mockgoose 测试通过我的速记查询/聚合管道(见下文第一个代码块)创建用于从一个集合中检索另一个集合中未引用的随机文档.

I'm trying to create some tests for my Mongoose models, and I can't figure out how to get the Jest/Mockgoose test to pass for a shorthand query/aggregation pipeline (see below in first code block) that I created to retrieve a random document from one collection that isn't referenced in another collection.

activitySchema.query.getUnused = function() {
    return Activity.aggregate()
        .lookup({
            from: 'userActivity',
            localField: '_id',
            foreignField: 'activity',
            as: 'matched_docs',
        })
        .match({ matched_docs: { $eq: [] } })
        .sample(1)
        .project({ matched_docs: 0, __v: 0 })
        .exec()
}

玩笑测试

describe('Activity methods', () => {
    test('Activity unused query executes', (done) => {
        function createActivity() {
            return Activity
                .create({ activity: 'running' })
                .then(activity => {
                    console.log('Activity is generated')
                    return Promise.resolve(true)
                })
        }
        async function retrieveActivity() {
            await createActivity()
            Activity
                .find()
                .getUnused()
                .then(unused => {
                    console.log(unused);
                    expect(unused).toHaveLength(1)
                    done()
                })
                .catch(x => {
                console.log(x)
                })
        }


        return retrieveActivity()
    })

})

沙盒节点JS代码:

Activity.find().getUnused()
    .then((x) => {
     console.log(x);
})

当我在沙箱节点文件中尝试它时,wit 正常工作并检索一个典型的查询集,如:

When I try it in a sandbox node file, wit works normally and retrieves a typical queryset like:

   [ { _id: 58f3dee3b0346910a69e6e5d, activity: 'running', __v: 0 } ]

当我运行测试时,我得到这个 MongoError:

When I run the test, I get this MongoError:

The 'cursor' option is required, except for aggregation explain

我如何以一种在两种情况下都适用的通用方式来解决这个问题?如果可能,我希望该方法返回一个承诺.我已经尝试了链式聚合方法(见上文)和更原生的 Mongo 管道聚合数组,但都返回错误.如果相关,我的 mongo 版本是 3.4.2.

How do I fix this with a universal manner that works in both contexts? I'd like for the method to return a promise if possible. I've tried both the chained aggregate methods (see above) and the more native Mongo aggregate array for pipelines, but both return errors. My mongo version is 3.4.2 if that's relevant.

推荐答案

终于想通了:我必须向聚合管道添加游标调用,并将其转换为流.为了维护我有查询方法的承诺返回一个在流结束后用数据解析的 Promise,如下所示:

Finally figured this out: I had to add a cursor call to the aggregation pipeline, and translate it into a stream. To maintain the Promise I had the query method return a Promise that resolves with data once the stream has ended, as below:

activitySchema.query.getUnused = function() {
    return new Promise((res, rej) => {
        let data = []
        return Activity.aggregate()
            .lookup({
                from: 'userActivity',
                localField: '_id',
                foreignField: 'activity',
                as: 'matched_docs',
            })
            .match({ matched_docs: { $eq: [] } })
            .sample(1)
            .project({ matched_docs: 0, __v: 0 })
            .cursor({})
            .exec()
            .on('data', doc => data.push(doc))
            .on('end', () => res(data))

    })
}

这篇关于Mongoose 聚合查询在 Jest/Mockgoose 测试中失败,可以在其他地方使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆