2016-06-09 214 views
0

喜歡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 
+0

您應該打印接收到的錯誤的堆棧跟蹤,它可以幫助找到問題所在。 – user3

+0

我沒有得到任何堆棧跟蹤,'[TypeError:無法讀取未定義的屬性'調用''這是我得到的所有 – goKishore

+0

嘗試'console.error(err.stack)'或甚至刪除catch,如果我記得正確摩卡打印堆棧跟蹤 – user3

回答

1

你需要要麼拋出捕獲物或取下捕獲物,這樣摩卡也會得到錯誤。目前,由於你發現錯誤,摩卡得到的承諾已經解決。

下面是我老的答案,這個問題改變之前 PS:它看起來像我誤會了是爲流星發佈這樣的波紋管的答案是不是真的正確


您遇到的錯誤因爲「mycollection」未發佈

我可能是因爲Meteor.publish('mycollection');is an async function,到集合尚未發佈時,當你測試它。

你應該做一個before()發佈測試

這裏之前是如何可以等待發布之前,在完成

+0

感謝指向我發佈,我忘了通過回調函數,編輯上面的代碼。 – goKishore

+0

我編輯了我的答案,以顯示我的意思的代碼示例,還要注意你添加的addObject,它是異步的,所以你應該返回一個承諾,告訴摩卡何時完成 – user3

+0

我再次編輯我的答案,爲你新問題,並補充說我誤解了事情 – user3

0

我在討論閱讀這樣的一個例子,這對我的作品,儘管有些人不鼓勵使用承諾的「完成」回調。

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); 
     }); 
    });