0
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:我在哪里做错了?

4

1 回答 1

0

不要从src. 您应该改用相对导入,在其中执行类似于

import { BodyToClass } from '../shared/decorators/body';

确保当代码被编译(移动到 JS 中dist)时,您不再需要该src目录存在

于 2020-05-15T16:29:53.923 回答