Jest.js 单元测试自动Map器

okxuctiv  于 2023-03-06  发布在  Jest
关注(0)|答案(1)|浏览(196)
jest.mock<CreateProductCommandHandler>(
  './commands/create.product.command.handler',
);
jest.mock<ProductRepository>('./infrastructure/product.repository');

describe('ProductController', () => {
  let productController: ProductController;
  let createProductCommandHandler: CreateProductCommandHandler;
  let productRepository: ProductRepository;
  let eventPublisher: EventPublisher;
  let commandBus: CommandBus;
  let queryBus: QueryBus;
  let mapper: Mapper;

  beforeEach(async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [AutomapperModule],
      controllers: [ProductController],
      providers: [
        CommandBus,
        QueryBus,
        AutomapperModule,
        CreateProductCommandHandler,
        {
          provide: ProductRepository,
          useValue: productRepository,
        },
        {
          provide: EventPublisher,
          useValue: eventPublisher
        },
        ProductRepository,
      ],
    }).compile();

    //mapper = moduleRef.get<Mapper>(typeof(Mapper));
    productController = moduleRef.get<ProductController>(ProductController);
    productRepository = moduleRef.get<ProductRepository>(ProductRepository);
    commandBus = moduleRef.get<CommandBus>(CommandBus);
    queryBus = moduleRef.get<QueryBus>(QueryBus);

    createProductCommandHandler = moduleRef.get<CreateProductCommandHandler>(
      CreateProductCommandHandler,
    );
    jest.clearAllMocks();
  });

下面是控制器类

@Controller()
export class ProductController {
  constructor(
    private readonly commandBus: CommandBus,
    private readonly queryBus: QueryBus,
    @InjectMapper() private readonly mapper: Mapper,
  ) {}

以下是自动Map器配置文件

@Injectable()
export class ProductProfile extends AutomapperProfile {
  constructor(@InjectMapper() mapper: Mapper) {
    super(mapper);
  }

  get profile(): MappingProfile {
    return (mapper: Mapper) => {
      createMap(mapper, ProductModel, CreateProductCommand);
      createMap(mapper, CreateProductCommand, ProductDomain);

      createMap(mapper, UpdateProductModel, UpdateProductCommand);
      createMap(mapper, UpdateProductCommand, ProductDomain);

      createMap(mapper, ProductModel, ProductDomain);
      createMap(mapper, ProductDomain, ProductModel);

      createMap(mapper, ProductDomain, ProductDocument);
      createMap(mapper, ProductDocument, ProductDomain);
      createMap(mapper, ProductDocument, ProductModel);
    };
  }

  protected get mappingConfigurations(): MappingConfiguration[] {
    return [
      extend(FzAbstractModel, FzAbstractCommand),

      extend(FzAbstractModel, FzAbstractDomain),
      extend(FzAbstractDomain, FzAbstractModel),

      extend(FzAbstractDomain, FzAbstractDocument),
      extend(FzAbstractDocument, FzAbstractDomain),
    ];
  }
}

我正在使用@nestjs/cqrs和一个服务类,其中自动Map器被注入到控制器和服务中,并尝试运行单元测试。
Map器实际上是来自@automapper/core的接口。
我需要一个帮助来模拟和解决自动Map器的依赖关系。因为当我运行测试时,它抛出了一个错误
Nest can't resolve dependencies of the ProductController (CommandBus, QueryBus, ?). Please make sure that the argument automapper:nestjs:default at index [2] is available in the RootTestModule context.

carvr3hs

carvr3hs1#

最后,在@MicaelLevi的帮助下,我实现了这个功能,模拟器可以很好地用于自动Map器。自动Map器配置文件也必须在providers部分传递。

let mapper: Mapper;

  beforeEach(async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [AutomapperModule],
      controllers: [ProductController],
      providers: [
        CommandBus,
        QueryBus,
        {
          provide: getMapperToken(),
          useValue: createMapper({
            strategyInitializer: classes(),
          }),
        },
        BaseProfile,
        ProductProfile,
        CreateProductCommandHandler,
        {
          provide: ProductRepository,
          useValue: productRepository,
        },
        {
          provide: EventPublisher,
          useValue: eventPublisher,
        },
        ProductRepository,
      ],
    }).compile();

    mapper = moduleRef.get<Mapper>(getMapperToken());

相关问题