我是摩卡正弦单元测试的新手。这就是我试图获得预期结果的原因。但这引发了以下错误。
AssertionError:已使用参数201调用了预期的存根
如果我做错了什么,有人可以帮我这个或任何建议。
async createUser(req: Request, res: Response, next: NextFunction) {
const { name, email, password } = req.body;
const user = new User({ name, email, password });
try {
const userCreated = await userService.createUserService(user);
res.status(201).json({ status: 201, data: userCreated });
} catch (err) {
next(httpErrors(500, err.message));
}
}
以上的单元测试:
import chai from "chai";
import sinon from "sinon";
import sinonChai from "sinon-chai";
import { Request, Response } from "express";
import userController from "./user.controller";
import UserService from "../services/user.service";
chai.use(sinonChai);
const { expect } = chai;
const mockReqData = {
name: "mockName",
email: "mockEmail",
password: "mockPassword#123"
};
const req = {
body: {
name: mockReqData.name,
email: mockReqData.email,
password: mockReqData.password
}
} as any;
const mockResponse = () => {
const res: any = {};
res.status = sinon.stub();
res.json = sinon.stub();
res.status.returns(res);
return res;
};
describe("UserController", () => {
let res;
let sandbox;
beforeEach(() => {
sandbox = sinon.createSandbox();
res = mockResponse();
});
afterEach(() => {
sandbox.restore();
});
it("should create user", async () => {
const stubValue = {
name: "StubName"
};
const stub = sandbox
.stub(userService, "createUserService")
.returns(stubValue);
await userController.createUser(req, res, () => {});
expect(res.status).to.have.been.calledWith(201);
});
});
任何帮助将不胜感激..
提前感谢。
这是单元测试解决方案:
controller.ts
:
import { NextFunction, Response, Request } from 'express';
import userService from './service';
import User from './model';
const userController = {
async createUser(req: Request, res: Response, next: NextFunction) {
const { name, email, password } = req.body;
const user = new User({ name, email, password });
try {
const userCreated = await userService.createUserService(user);
res.status(201).json({ status: 201, data: userCreated });
} catch (err) {
next(err);
}
},
};
export default userController;
service.ts
:
import User from './model';
const userService = {
async createUserService(user: User) {
return {};
},
};
export default userService;
model.ts
:
export default class User {
constructor(data) {}
}
controller.test.ts
:
import userController from './controller';
import { Request } from 'express';
import userService from './service';
import sinon from 'sinon';
import User from './model';
describe('userController', () => {
it('should create user', async () => {
const mReq = { body: { name: 'mockName', email: 'mockEmail', password: 'mockPassword#123' } } as Request;
const mRes = { status: sinon.stub().returnsThis(), json: sinon.stub() } as any;
const mNext = sinon.stub();
const stubValue = {
name: 'StubName',
};
const createUserServiceStub = sinon.stub(userService, 'createUserService').resolves(stubValue);
await userController.createUser(mReq, mRes, mNext);
sinon.assert.calledWithExactly(
createUserServiceStub,
new User({
name: 'mockName',
email: 'mockEmail',
password: 'mockPassword#123',
}),
);
sinon.assert.calledWithExactly(mRes.status, 201);
sinon.assert.calledWithExactly(mRes.json, { status: 201, data: { name: 'StubName' } });
});
});
带有覆盖率报告的单元测试结果:
userController
✓ should create user
1 passing (25ms)
---------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------|---------|----------|---------|---------|-------------------
All files | 85.71 | 100 | 66.67 | 85.71 |
controller.ts | 90 | 100 | 100 | 90 | 14
model.ts | 100 | 100 | 100 | 100 |
service.ts | 66.67 | 100 | 0 | 66.67 | 5
---------------|---------|----------|---------|---------|-------------------