在 nodejs (JavaScript) 装饰器上编写 junit 测试用例时如何初始化?找不到我的服装装饰器(正文到 class)
How do I initialized while write junit testcase on nodejs (JavaScript) decorators ? my costume decorator (body to class) is not find
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 测试用例时如何初始化?找不到我的服装装饰器(正文 class)。
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();
});
});
});
当我尝试执行此测试时class(UserController.spec.ts Node.js 我收到以下错误
import { BodyToClass } from 'src/shared/decorators/body' 找不到模块,虽然我已经导入了包,所以
所以,我的问题是:
1:我们如何在编写测试用例时模拟装饰器。
2:do 我们需要初始化或模拟“@BodyToClass()”?
3:我哪里做错了?
不要从 src
导入。您应该改用相对导入,在其中执行类似于
的操作
import { BodyToClass } from '../shared/decorators/body';
确保在编译代码时(移至 dist
中的 JS)您不再需要存在 src
目录
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 测试用例时如何初始化?找不到我的服装装饰器(正文 class)。
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();
});
});
});
当我尝试执行此测试时class(UserController.spec.ts Node.js 我收到以下错误
import { BodyToClass } from 'src/shared/decorators/body' 找不到模块,虽然我已经导入了包,所以
所以,我的问题是:
1:我们如何在编写测试用例时模拟装饰器。 2:do 我们需要初始化或模拟“@BodyToClass()”? 3:我哪里做错了?
不要从 src
导入。您应该改用相对导入,在其中执行类似于
import { BodyToClass } from '../shared/decorators/body';
确保在编译代码时(移至 dist
中的 JS)您不再需要存在 src
目录