喜歡Louis提出這個問題是關係到Using Promises to test Meteor - Mocha承諾在摩卡單元測試
,我已經複製了一個小程序同樣的問題,這樣就可以重現此。在這一個也是摩卡不關心斷言的說法。承諾的catch塊得到這個錯誤。
/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
您應該打印接收到的錯誤的堆棧跟蹤,它可以幫助找到問題所在。 – user3
我沒有得到任何堆棧跟蹤,'[TypeError:無法讀取未定義的屬性'調用''這是我得到的所有 – goKishore
嘗試'console.error(err.stack)'或甚至刪除catch,如果我記得正確摩卡打印堆棧跟蹤 – user3