[英]Mocha Does Not Fail When Tests Wrapped in describe()
我有相对简单的mocha&chai测试设置。 不幸的是,当我运行摩卡时,测试肯定会失败! 这是我的测试用例。
var expect = require('chai').expect;
var nock = require('nock');
var request = require('request');
var testUrl = 'http://test.wicked.ti';
var getItems = function(url, callback) {
request.get(url + '/items',function(err, data) {
if(err) throw err;
callback(data);
});
};
describe('Sample Unit Tests', function(){
it('I am making sure the correct rest endpoint is called', function() {
var request = nock(testUrl)
.get('/items')
.reply(200, {});
getItems(testUrl, function(data) {
console.log(data); // This runs
expect(true).to.equal(false); // This should always fail!
done();
});
});
});
在try catch中包含expect(true).to.equal(false)
会抛出一个错误(如下所示)。 那是
it('I am making sure the correct rest endpoint is called', function() {
var request = nock(testUrl)
.get('/items')
.reply(200, {});
getItems(testUrl, function(data) {
console.log(data); // This runs
// Adding try/catch block
try { expect(true).to.equal(false); } catch(err) { console.error(err) }
done();
});
这是记录的错误
{ [AssertionError: expected true to equal false]
message: 'expected true to equal false',
showDiff: true,
actual: true,
expected: false }
我一直绞尽脑汁想弄清楚我可能做错了什么但没有成功! 问题是我错过了什么? 如果有任何帮助,我试图在describe()
和it()
块之外写这个,它运行正常。
那是因为您正在运行同步测试,因此它不会等待异步功能完成。 要使其异步,您的回调需要一个参数:
it('...', function(done) {
// |
// |
// this is where "done" comes from and it's
// the missing bug in your code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.