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时,出现以下错误
从"src/shared/decorators/body"导入{ BodyToClass }找不到模块,虽然我已经导入了包,所以
所以,我的问题是:
1:我们如何在编写测试用例时模拟装饰器。 2:我们需要初始化还是模拟"@BodyToClass(("? 3: 我哪里做错了?
不要从src
导入。您应该改用相对导入,在那里您可以执行类似于
import { BodyToClass } from '../shared/decorators/body';
确保在编译代码时(在dist
中移动到JS(,您不再需要src
目录存在