【发布时间】:2016-12-14 07:20:14
【问题描述】:
以下是我用 mocha、chai 和 supertest 编写的代码。我对下面有效的代码段有疑问,重点是令牌。
describe('Authenticated userTest', function () {
var token;
before(function loginAuth(done) {
request(app)
.post("/login/local")
.send("username=testName")
.send("password=qwe123QWE")
.expect(function (res) {
should.exist(res.body.token);
token = res.body.token;
})
.end(done);
});
it('should give me a defined token', function(done) {
console.log("token is " + token);
done();
});
});
显然,令牌在这里定义得很好。但是,当我按如下方式删除完成功能时:
describe('Authenticated userTest', function () {
var token;
before(function loginAuth() { //done is removed here
request(app)
.post("/login/local")
.send("username=testName")
.send("password=qwe123QWE")
.expect(function (res) {
should.exist(res.body.token);
token = res.body.token;
})
.end(); //done is removed here
});
it('should give me a defined token', function(done) {
console.log("token is " + token);
done();
});
});
令牌变得未定义。据我了解,done 是一个从 before 钩子传递到其后从内置源代码中以 it(...) 开始的所有各种测试的函数。
因此,我想澄清这个特定问题(如果 done 仅在测试中传递;如果 done 仅接受 err 参数)以及为什么在删除 done 参数后令牌变得未定义?
谢谢。
【问题讨论】:
标签: node.js mocha.js chai supertest