【问题标题】:Supertest/mocha done parameter passed within tests在测试中传递的 Supertest/mocha done 参数
【发布时间】: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


    【解决方案1】:

    令牌没有变得未定义...在您尝试使用它时,它仍然未定义。 token 尚未设置,因为 mocha 不知道它正在使用异步测试。

    https://justinbellamy.com/testing-async-code-with-mocha/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 2015-03-03
      • 2023-03-13
      • 1970-01-01
      相关资源
      最近更新 更多