import { BodyToClass } from 'src/shared/decorators/body';
import { UserService } from './user.service';
@Put()
async updateUser(@BodyToClass() user: UpsertUserDto): Promise<UpsertUserDto> {`enter code here`
try {
if (!user.id) {
throw new BadRequestException('User Id is Required');
}
return await this.userService.updateUser(user);
} catch (e) {
throw e;
}
}`
在 nodejs (JavaScript) 装饰器上编写 junit 测试用例时如何初始化?找不到我的服装装饰师(身体上课)。
import { BodyToClass } from 'src/shared/decorators/body';
import { Test, TestingModule } from '@nestjs/testing';
import { UserDto } from 'src/shared/interfaces/dto/user.dto';
import { UserController } from './user.controller';
import { UserService } from './user.service';
jest.mock('./user.service');
// jest.mock('src/shared/decorators/body');
describe('App Controller', () => {
let testingModule: TestingModule;
let controller: UserController;
let service: UserService;
beforeEach(async () => {
testingModule = await Test.createTestingModule({
controllers: [UserController],
providers: [
{
provide: UserService,
useFactory: () => ({
getUserById: jest.fn(() => true),
}),
},
// provide: 'BodyToClass',
// useClass:BodyToClass
],
}).compile();
controller = testingModule.get(UserController);
service = testingModule.get(UserService);
});
describe('update user', () => {
it('it should update the users', async () => {
expect(service.updateUser).toHaveReturned();
});
});
});
当我尝试执行这个 Testclass(UserController.spec.ts Node.js 我收到以下错误
import { BodyToClass } from 'src/shared/decorators/body' 找不到模块,虽然我已经导入包,所以
所以,我的问题是:
1:我们如何在编写测试用例时模拟装饰器。2:我们需要初始化或模拟“@BodyToClass()”吗?3:我在哪里做错了?