【发布时间】:2014-12-31 00:04:53
【问题描述】:
我有一些 Sails.js API 测试(使用 Mocha)利用 SuperTest 的 .end() 方法在响应上运行一些 Chai 断言。
我在断言之后调用测试的done() 回调,但如果抛出断言错误,测试就会超时。
我可以将断言包装在 try/finally 中,但这似乎有点恶心:
var expect = require('chai').expect;
var request = require('supertest');
// ...
describe('should list all tasks that the user is authorized to view', function () {
it('the admin user should be able to see all tasks', function (done) {
var agent = request.agent(sails.hooks.http.app);
agent
.post('/login')
.send(userFixtures.admin())
.end(function (err, res) {
agent
.get('/api/tasks')
.expect(200)
.end(function (err, res) {
try {
var tasks = res.body;
expect(err).to.not.exist;
expect(tasks).to.be.an('array');
expect(tasks).to.have.length.of(2);
} finally {
done(err);
}
});
});
});
});
关于如何更好地处理这个问题的任何建议?也许Chai HTTP 会更好?
【问题讨论】:
标签: javascript sails.js mocha.js chai supertest