摩卡单元测试中的承诺 [英] Promises in Mocha Unit Tests

查看:89
本文介绍了摩卡单元测试中的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与使用承诺测试流星-摩卡咖啡

就像路易斯建议的那样,我已经在一个较小的程序中复制了相同的问题,以便您可以重现此问题.在这一点上,Mocha也不在乎assert语句. promises的catch块会收到此错误.

Like Louis suggested, I have replicated the same issue in a smaller program, so that you can reproduce this. And in this one too Mocha doesn't care about the assert statement. The catch block of the promises gets this error.

/server/main.js

import { Meteor } from 'meteor/meteor';

export const myCollection = new Mongo.Collection('mycollection');

export const addObject = function (id) {
    myCollection.insert({
        name: 'test ' + id
    });
}

Meteor.publish('mycollection', function() {
    return myCollection.find({});
});

/server/main.test.js

/**
 * Created by enigma on 6/9/16.
 */
import { Meteor } from 'meteor/meteor';
import { PublicationCollector } from 'meteor/johanbrook:publication-collector';
import { Promise } from 'meteor/promise';
import { assert } from 'meteor/practicalmeteor:chai';
import { Random } from 'meteor/random';
import { addObject } from '../server/main.js';

if (Meteor.isServer) {
    describe('test mocha promise', function() {
        before(function() {
            addObject(Random.id());
        });
        it('collects  myCollection test', function() {
            const collector = new PublicationCollector({ userId: Random.id()});

            return new Promise(function(resolve) {
                    collector.collect('mycollection', function (collections) {
                        resolve(collections);
                    });
            }).then(function(coll) {
                chai.assert.notEqual(coll, null);
                chai.assert.equal(coll, null);
            }).catch(function(err) {
                console.log('error:', err.stack);
            });
        });
    });
}

控制台输出

=> Meteor server restarted
I20160609-18:31:14.546(-5)? MochaRunner.runServerTests: Starting server side tests with run id GK3WqWY4Ln9u6vmsg
I20160609-18:31:14.598(-5)? error: AssertionError: expected { Object (mycollection) } to equal null
I20160609-18:31:14.598(-5)?     at Function.assert.equal (packages/practicalmeteor_chai.js:2635:10)
I20160609-18:31:14.598(-5)?     at test/main.test.js:25:29
I20160609-18:31:14.598(-5)?     at /Users/enigma/.meteor/packages/promise/.0.6.7.1d67q83++os+web.browser+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:33:40
W20160609-18:31:14.607(-5)? (STDERR) MochaRunner.runServerTests: failures: 0

推荐答案

您需要扔掉渔获物或将渔获物移走,这样摩卡咖啡也会收到错误.目前,由于您发现了错误,因此摩卡咖啡的承诺得到了解决.

you need to either throw in the catch or remove the catch, so that mocha gets the error too. currently since you catch the error, the promise mocha gets is resolved.

下面是我的老答案,在问题改变之前 ps:看来我误解了流星的发行,所以下面的答案并不正确

Below was my old answer, before the question change ps: it looks like I misunderstood what was a publish for meteor so the bellow answer is not really correct

您遇到的错误是因为"mycollection"未发布

The error you're encountering is because "mycollection" is not published

我可能是因为Meteor.publish('mycollection'); 是一个异步函数,测试时尚未发布该收藏夹.

I is probably because Meteor.publish('mycollection'); is an async function, to the collection is not published yet when you test it.

您应该在测试前在before()中进行发布

you should do the publish in a before() before your test

这是一个示例,您可以如何等待发布完成

Here is an example of how you can wait the publish to finish in a before

这篇关于摩卡单元测试中的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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