【问题标题】:done() called within superagent request not working在超级代理请求中调用的 done() 不起作用
【发布时间】:2018-02-12 03:29:22
【问题描述】:

我正在创建一个具有登录后发布功能的应用程序。我在 mocha 中编写了以下测试代码。我收到此错误:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

我认为我在使用 done() 时犯了一些错误。所有其他断言语句都已通过。如果您需要更多信息,请告诉我哪里出错了。

it("Login and post", function(done) {
    superagent
        .post(URL_ROOT + "/register")
        .send({
            email: "posttest@test.com",
            password: "posttest"
        })
        .end(function(err, res) {
            assert.ifError(err);
            var token = res.body.token;
            superagent
                .post(URL_ROOT + "/post")
                .send({
                    content: "testing post",
                    user: jwt.decode(token).id
                })
                .set("Authorization", "test_scheme " + token)
                .end(function(err, res){
                    assert.ifError(err);
                    assert.equal(res.body.post.content, "testing post");
                    done();
                });
        });
});

【问题讨论】:

    标签: javascript node.js testing mocha.js superagent


    【解决方案1】:

    您的测试可能需要超过 2 秒的时间才能运行:在您对 it 的回调的第一行中,就在 superagent .post(URL_ROOT + "/register")... 之前添加以下行:

    this.timeout(30000)

    这允许您仅在 30 秒后超时,而不是 2 秒。

    【讨论】:

      猜你喜欢
      • 2021-07-03
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      • 2017-10-22
      相关资源
      最近更新 更多