2015-01-05 68 views
1

我試圖運行一個chai測試,它使用貓鼬連接到mongodb,但它與'預期未定義爲對象'失敗。我使用的是我在功能應用程序中使用的相同方法。我是否正確連接到數據庫?我可以從測試中連接貓鼬嗎?

var expect = require('chai').expect; 
var eeg = require('../eegFunctions'); 
var chai = require("chai"); 
var chaiAsPromised = require("chai-as-promised"); 
chai.use(chaiAsPromised); 
var mongoose = require('mongoose'); 
var db = mongoose.connection; 

db.on('error', console.error); 
db.once('open', function callback(){console.log('db ready');}); 

mongoose.connect('mongodb://localhost/eegControl'); 

test("lastShot() should return an object", function(){ 

    var data; 
    eeg.lastShot(function(eegData){ 
     data = eegData; 
    }); 
    return expect(data).to.eventually.be.an('object');   

}); 
+0

你得到這個錯誤是什麼行?編輯:沒關係...回答來... – jakerella

+0

你使用了什麼其他測試框架?我的意思是,「測試」是什麼? – lante

回答

1

test is asynchronous因爲蒙戈的連接是異步的,所以你需要做的斷言發生的連接完成時:

test("lastShot() should return an object", function(done){ // Note the "done" argument 

    var data; 
    eeg.lastShot(function(eegData){ 
     data = eegData; 

     // do your assertions in here, when the async action executes the callback... 
     expect(data).to.eventually.be.an('object'); 

     done(); // tell Mocha we're done with async actions 
    }); 

    // (no need to return anything) 
});