【发布时间】:2021-12-19 12:11:21
【问题描述】:
我想测试一些 addUser() 方法已被调用并返回值。但我收到此错误:“No overload matches this call. Overload 1 of 4, '(object: typeof UserController, method: never): SpyInstance<never, never>', gave the following error.
所以我的 userController.spec.ts 错误在 jest.spyOn(...) 中:
import {UserController} from "../../src/modules/Users/Infrastructure/http/controllers";
describe('Users Controllers series ', function () {
it("should create a new user", async () => {
const spy = jest.spyOn(UserController, "addUser").mockReturnValueOnce()
})
});
然后这里是 userController.ts
import { UserServices} from "../../../domain/services";
import {CreateUSerDTO} from "../../../domain/dto/createUserDTO";
import express from "express";
import { Response} from "../../../../../shared/helpers/response";
export class UserController {
constructor(private readonly userService: UserServices) {}
async addUser(req: express.Request, res: express.Response):Promise<Response<CreateUSerDTO>> {
let dto:CreateUSerDTO = req.body as CreateUSerDTO
dto = {
username:dto.username,
useremail: dto.useremail,
password: dto.password
}
try {
const result = await this.userService.createUser(dto)
console.log(res.json(result))
return Response.ok<CreateUSerDTO>(result)
}catch (error) {
return Response.fail<CreateUSerDTO>(error)
}
}
}
让我知道我的代码出了什么问题
【问题讨论】:
标签: node.js typescript unit-testing jestjs