2015-01-05 31 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) 
});