【发布时间】:2016-03-06 00:54:20
【问题描述】:
我正在使用 Mocha 和 chai 运行一些测试。其中一项测试挂在.should.be.deep.equal 电话上。
这是测试代码:
// Make the fake connection before running tests
before(function(done) {
mongoose.connect('mongodb://fake.test/TestingDB', function(err) {
done(err);
});
});
// Test Cases
describe('Testing the functions that deal with users and locations:', function() {
// Test Setup
var req = {};
beforeEach(function(done) {
mockgoose.reset()
async.parallel([function(callback){
sensors.create(testData.deviceData, function(err, model) {
if (err) {console.log(err)}
callback();
});
}, function(callback) {
locations.create(testData.locationData, function(err, model) {
if (err) {console.log(err)}
callback();
});
}], function(err) {
done();
});
});
afterEach(function(done) {
mockgoose.reset();
done();
});
// Tests
describe('function locationList', function() {
it('should list the test location', function(done) {
dbFunctions.locationList(req, function(result) {
console.log(result) //Prints the whole result
console.log(testData.locationList)
result.should.exist; //This doesn't cause it to hang
result.should.be.deep.equal(testData.locationList) //hangs here
done(result);
});
})
})
});
这是它正在测试的功能:
exports.locationList = function(req, callback) {
listLocations().then(
function(data) {
callback(data);
},
function(err) {
console.log('Error Retrieving Location Information: ' + err);
callback(err);
});
};
正如我在 cmets 中所指出的,结果对象存在并被打印到控制台。 results.should.exist; 不会抛出异常,如果我注释掉所有内容,但测试工作正常。出于某种奇怪的原因,尽管 testData.locationList 和 result 对象都存在,但测试超时。我有 14 个其他测试使用完全相同的语法,没有任何问题。有谁知道是什么原因导致此特定测试发生这种情况?
这是测试的输出:
Testing the functions that deal with users and locations:
function locationList
[ { devices: {},
address: '123 Fake St, Waterloo, On',
location: 'Unittest',
owner: 'unit@test.com',
_id: '-1' } ]
[ { devices: {},
address: '123 Fake St, Waterloo, On',
location: 'Unittest',
owner: 'unit@test.com',
_id: '-1' } ]
1) should list the test location
0 passing (2s)
1 failing
1) Testing the functions that deal with users and locations: function locationList should list the test location:
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
at null.<anonymous> (C:\Users\My Name\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:189:19)
延长超时不起作用。也不会随机放置一些东西(即整数 1 在 .should.be.deep.equal() 函数中。
【问题讨论】:
-
我认为这不能解决您的问题,但您可能应该只调用 done() 而不是 done(result) 因为传递给 done() 的任何内容都将被解释为错误。
标签: node.js unit-testing mongoose mocha.js chai