Mocha/Chai 测试挂在 .should.be.deep.equal 尽管结果存在
Mocha/Chai tests hanging at .should.be.deep.equal despite result existing
我正在 运行 使用 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);
});
};
正如我在评论中指出的那样,结果对象存在并打印到控制台。 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)
延长超时时间无效。也不会随机放置一些东西(即 .should.be.deep.equal()
函数中的整数 1
。
一个猜测:也许问题出在用它自己的 functions/members 装饰 result
的猫鼬,当 chai 试图枚举它们来执行时,其中一个不知何故陷入了无限循环深度对比
我的猜测是 exports.locationList
中的回调是同步调用的,并且您的测试用例实际上失败了,抛出了一个异常,该异常永远不会被捕获,因为回调是从承诺中(同步)调用的处理程序(更多信息 here)。
试试看这是否更好:
dbFunctions.locationList(req, function(result) {
setImmediate(function() {
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);
});
});
// or use a module like dezalgo (https://github.com/npm/dezalgo)
根本原因可能是 mockgoose
。
此外,您没有使用正确的 Node.js 约定,其中回调函数的第一个参数是 "reserved" 表示错误。换句话说,您的代码应该类似于:
if (err) {
callback(err);
} else {
callback(null, data);
}
您现在将错误 [=25=] 和 数据作为第一个参数传递。
我正在 运行 使用 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);
});
};
正如我在评论中指出的那样,结果对象存在并打印到控制台。 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)
延长超时时间无效。也不会随机放置一些东西(即 .should.be.deep.equal()
函数中的整数 1
。
一个猜测:也许问题出在用它自己的 functions/members 装饰 result
的猫鼬,当 chai 试图枚举它们来执行时,其中一个不知何故陷入了无限循环深度对比
我的猜测是 exports.locationList
中的回调是同步调用的,并且您的测试用例实际上失败了,抛出了一个异常,该异常永远不会被捕获,因为回调是从承诺中(同步)调用的处理程序(更多信息 here)。
试试看这是否更好:
dbFunctions.locationList(req, function(result) {
setImmediate(function() {
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);
});
});
// or use a module like dezalgo (https://github.com/npm/dezalgo)
根本原因可能是 mockgoose
。
此外,您没有使用正确的 Node.js 约定,其中回调函数的第一个参数是 "reserved" 表示错误。换句话说,您的代码应该类似于:
if (err) {
callback(err);
} else {
callback(null, data);
}
您现在将错误 [=25=] 和 数据作为第一个参数传递。