[英]Promises in Mocha Unit Tests
这个问题与Using Promises to test Meteor - Mocha 有关
就像 Louis 建议的那样,我在一个较小的程序中复制了相同的问题,以便您可以重现它。 在这一点中,Mocha 也不关心 assert 语句。 承诺的 catch 块收到此错误。
/服务器/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({});
});
/服务器/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
您需要投入捕获或移除捕获,以便 mocha 也会收到错误。 目前,由于您发现了错误,因此 mocha 得到的承诺已解决。
以下是我在问题更改之前的旧答案 ps:看起来我误解了流星的发布所以下面的答案并不正确
您遇到的错误是因为“mycollection”未发布
我可能是因为Meteor.publish('mycollection');
是一个异步函数,当你测试它时,集合还没有发布。
你应该在测试之前在before()
发布
这是一个示例,说明如何在之前等待发布完成
我在讨论中读到了这个,这对我有用,尽管有些人不鼓励使用带有承诺的“完成”回调。
it('collects myCollection test', function(done) {
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);
done();
}).catch(function(err) {
done(err);
});
});
我像这样使用 PublicationCollector:
it('should publish 2 documents', async () => {
const collector = new PublicationCollector({ 'userId': Random.id() });
const testPromise = new Promise((resolve, reject) => {
collector.collect('myDocuments',
(collections) => {
resolve(collections.myDocuments.length);
});
});
const result = await testPromise;
assert.equal(result, 1);
});
从这里改编。
此测试相对优雅地失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.