【问题标题】:What is causing this Mocha test to timeout?是什么导致此 Mocha 测试超时?
【发布时间】:2020-10-12 20:30:15
【问题描述】:

我正在使用 React、Redux、Express、MongoDB 和 Mocha 开发一个全栈 Web 应用程序,并且在编写处理使用 Sinon 模拟/存根 Mongoose 模型的测试时遇到了麻烦。目前我正在尝试测试此路由处理程序以创建用户帐户:

import md5 from "md5";

import { User } from "../models";
import { UserCreationStatuses } from "../../app/store/action-types";

export async function postCreateUser(req, res) {
  let { username, password } = req.body;
  if (await User.exists({ name: username })) {
    return res.status(500).send({ reason: UserCreationStatuses.USERNAME_TAKEN });
  } else {
    let newUser = new User({
      name: username,
      passwordHash: md5(password),
    });
    newUser.save((err) => {
      if (err) {
        console.info("Error in user creation route:\n", err);
        return res.status(500).send({ reason: UserCreationStatuses.SERVER_ERROR });
      }
      return res.status(200).send();
    });
  }
}

这是我目前陷入困境的测试(有一些省略号,我省略了其他测试)。我假设我通过为我的 Mongoose 模型“用户”存根 exists 函数引起了问题:

import { assert, expect } from "chai";
let chai = require("chai");
let should = require("chai").should();
import httpMocks from "node-mocks-http";
import sinon from "sinon";

import { User } from "../models";
import { UserCreationStatuses } from "../../app/store/action-types";
import { postCreateUser } from "../route-handlers/user-creation";

describe("Route Handlers", function () {

    describe("User Creation", function () {

        beforeEach(function () {
            sinon.stub(User, "exists");
        });

        afterEach(function () {
            User.exists.restore();
        });

        ...

        it("should respond with a code of 500 when provided with an existing username, and any password", async function (done) {
            this.timeout(10000);

            let req = httpMocks.createRequest({
                method: "POST",
                url: "/create-user",
                body: {
                    username: "ExistingUser",
                    password: "AnyPassword"
                }
            });
            let res = httpMocks.createResponse();

            User.exists.resolves(true);

            await postCreateUser(req, res);
            res.status.should.equal(500);
            done();
        });

    });

    ...

});

在我的测试输出中,“应该以 500 的代码响应”测试给出了这个错误:

Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (F:\Web\what-about-a-band-called\src\server\route-handlers\route-handlec.js)

我曾尝试延长 Mocha 的超时时间,正如您在测试中看到的那样,但这并没有改变任何东西。关于发生了什么的任何想法?我对使用 Sinon 存根方法非常陌生,我假设我直接跳入异步方法和 Mongoose 有点过头了。另外,here is the full repo 以防这里没有足够的线索。

【问题讨论】:

    标签: node.js mongoose mocha.js sinon


    【解决方案1】:

    所以,经过更多的故障排除后,我设法让它工作,但我对它为什么会这样感到困惑!首先,我在断言中使用res.status 是错误的,我应该一直使用.statusCode。我现在感到困惑的部分是,解决真正问题的方法是既不调用done(),也不将其作为参数传递给it 的回调。引入.statusCode而不是.status后,出现如下错误:

    Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.
    

    删除done() 调用但在回调参数中保留done 后,我得到以下信息:

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

    删除done 完全解决了问题!那么,我认为问题在于await postCreateUser(req, res); 是否正确(或接近正确?)?该调用是否既返回又解决了一个承诺?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 2015-09-30
      • 2012-12-03
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      相关资源
      最近更新 更多