嵌套承诺 - 摩卡咖啡 - 超时
Nested promises - Mocha - Exceeded timeout
由于使用 Mocha 超时,我的测试失败了。
我确实调用了 "done()" 函数,但由于某种原因它似乎不起作用。
我的测试目前是这样的:
var express = require('express');
var expect = require('chai').expect;
var mocha = require('mocha');
var calendar = require('./../Server/calendarDatabase');
describe("Calendar", function () {
describe("Database", function () {
it("should get stuff from the database", function (done) {
return calendar.Connect()
.then(function () {
return calendar.getAll();
})
.then(function (returnValue) {
expect(returnValue.count).to.equal(5); //This should fail, my database records are 20+
done();
});
});
});
});
我的 calendar.Connect()
和 calendar.getAll()
都是承诺:
var express = require('express');
var sql = require('mssql');
var config = require('./../config');
var calendarDbConnection = {};
calendarDbConnection.Connect = function() {
return sql.connect(config.mssql);
}
calendarDbConnection.getAll = function () {
var promise = new sql.Request()
.query('select * from CalendarTable')
.catch(function (err) {
console.log(err.message);
});
return promise;
}
module.exports = calendarDbConnection;
然而,在 运行 我的测试中,我得到以下输出:
当我在最后一个 then()
之后调用 done()
时,函数得到解析,但我的测试结果没有。我从数据库中获取的行数超过 20,我检查它们是否等于 5。所以,我的测试应该失败,但它没有。
//previous code
.then(function (returnValue) {
expect(returnValue.count).to.equal(5); //This should fail, my database records are 20+
});
done();
//...
所以最后一个结果通过了我的测试,但它不应该。
我在这里错过了什么?我正在调用回调函数,但我的预期结果不正确。
因为你是 return测试中的 Promise,你 should not 将 done
作为参数传递:
Alternately, instead of using the done() callback, you may return a Promise. This is useful if the APIs you are testing return promises instead of taking callbacks.
尽管您可以如上所述将 done
传递给 catch
调用,但摆脱 done
并仅 return 承诺似乎更方便。
it("should get stuff from the database", function () {
return calendar.Connect() // returns Promise, so no `done`
.then(function () {
return calendar.getAll();
})
.then(function (returnValue) {
expect(returnValue.count).to.equal(5);
});
});
只在接球时传球。
then(....) { ... done(); }.catch(done);
由于使用 Mocha 超时,我的测试失败了。 我确实调用了 "done()" 函数,但由于某种原因它似乎不起作用。
我的测试目前是这样的:
var express = require('express');
var expect = require('chai').expect;
var mocha = require('mocha');
var calendar = require('./../Server/calendarDatabase');
describe("Calendar", function () {
describe("Database", function () {
it("should get stuff from the database", function (done) {
return calendar.Connect()
.then(function () {
return calendar.getAll();
})
.then(function (returnValue) {
expect(returnValue.count).to.equal(5); //This should fail, my database records are 20+
done();
});
});
});
});
我的 calendar.Connect()
和 calendar.getAll()
都是承诺:
var express = require('express');
var sql = require('mssql');
var config = require('./../config');
var calendarDbConnection = {};
calendarDbConnection.Connect = function() {
return sql.connect(config.mssql);
}
calendarDbConnection.getAll = function () {
var promise = new sql.Request()
.query('select * from CalendarTable')
.catch(function (err) {
console.log(err.message);
});
return promise;
}
module.exports = calendarDbConnection;
然而,在 运行 我的测试中,我得到以下输出:
当我在最后一个 then()
之后调用 done()
时,函数得到解析,但我的测试结果没有。我从数据库中获取的行数超过 20,我检查它们是否等于 5。所以,我的测试应该失败,但它没有。
//previous code
.then(function (returnValue) {
expect(returnValue.count).to.equal(5); //This should fail, my database records are 20+
});
done();
//...
所以最后一个结果通过了我的测试,但它不应该。
我在这里错过了什么?我正在调用回调函数,但我的预期结果不正确。
因为你是 return测试中的 Promise,你 should not 将 done
作为参数传递:
Alternately, instead of using the done() callback, you may return a Promise. This is useful if the APIs you are testing return promises instead of taking callbacks.
尽管您可以如上所述将 done
传递给 catch
调用,但摆脱 done
并仅 return 承诺似乎更方便。
it("should get stuff from the database", function () {
return calendar.Connect() // returns Promise, so no `done`
.then(function () {
return calendar.getAll();
})
.then(function (returnValue) {
expect(returnValue.count).to.equal(5);
});
});
只在接球时传球。
then(....) { ... done(); }.catch(done);