作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
This is my controller class(usercontoller.ts) i am trying to write junit test cases for this class
import { UpsertUserDto } from '../shared/interfaces/dto/upsert-user.dto';
import { UserDto } from '../shared/interfaces/dto/user.dto';
import { UserService } from './user.service';
async updateUser(@BodyToClass() user: UpsertUserDto): Promise<UpsertUserDto> {
try {
if (!user.id) {
throw new BadRequestException('User Id is Required');
}
return await this.userService.updateUser(user);
} catch (e) {
throw e;
}
}
jest.mock('./user.service');
describe('User Controller', () => {
let usercontroller: UserController;
let userservice: UserService;
// let fireBaseAuthService: FireBaseAuthService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UserController],
providers: [UserService]
}).compile();
usercontroller = module.get<UserController>(UserController);
userservice = module.get<UserService>(UserService);
});
afterEach(() => {
jest.resetAllMocks();
});
describe('update user', () => {
it('should return a user', async () => {
//const result = new UpsertUserDto();
const testuser = new UpsertUserDto();
const mockDevice = mock <Promise<UpsertUserDto>>();
const mockNumberToSatisfyParameters = 0;
//const userservice =new UserService();
//let userservice: UserService;
jest.spyOn(userservice, 'updateUser').mockImplementation(() => mockDevice);
expect(await usercontroller.updateUser(testuser)).toBe(mockDevice);
it('should throw internal error if user not found', async (done) => {
const expectedResult = undefined;
****jest.spyOn(userservice, 'updateUser').mockResolvedValue(expectedResult);****
await usercontroller.updateUser(testuser)
.then(() => done.fail('Client controller should return NotFoundException error of 404 but did not'))
.catch((error) => {
expect(error.status).toBe(503);
expect(error.message).toMatchObject({error: 'Not Found', statusCode: 503}); done();
});
});
});
});
最佳答案
您的 UserService
很有可能类具有其他依赖项,因此,Nest 无法实例化 UserService
类(class)。当你试图做 userService = module.get(UserService)
您正在检索 undefined
因此关于 jest.spyOn()
的错误.在单元测试中,您应该提供一个模拟提供程序来代替您的实际提供程序,如下所示:
describe("User Controller", () => {
let usercontroller: UserController;
let userservice: UserService;
// let fireBaseAuthService: FireBaseAuthService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UserController],
providers: [
{
provide: UserService,
useValue: {
updateUser: jest.fn(),
// other UserService methods
}
}
],
}).compile();
usercontroller = module.get<UserController>(UserController);
userservice = module.get<UserService>(UserService);
});
// rest of tests
});
UserService
您将得到一个具有正确功能的对象,然后可以是
jest.spyOn
编辑和 mock
关于javascript - 开 Jest : Cannot spy the property because it is not a function; undefined given instead getting error while executing my test cases,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61868153/
我是一名优秀的程序员,十分优秀!