【发布时间】: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