0

有谁知道使用Crud 库在 NestJS 上测试服务的正确方法是什么?

这是我的服务测试类:

describe('AddressService', () => {
    let module: TestingModule;
    let addressService: AddressService;
    let addressRepositoryMock: MockType<Repository<Address>>;
    const req: CrudRequest = { parsed: null, options: null };
    const createDto: Address = {Omitted: ''};
    const addressMock: Address = {Omitted: ''};

    beforeEach(async () => {
        module = await Test.createTestingModule({
            providers: [
                AddressService,
                {
                    provide: getRepositoryToken(Address),
                    useFactory: repositoryMockFactory,
                },
            ],
        }).compile();

        addressService = module.get<AddressService>(AddressService);
        addressRepositoryMock = module.get(getRepositoryToken(Address));
    });

    afterEach(() => {
        jest.resetAllMocks();
    });

    describe('create Address', () => {
        it('should calls addressRepository.save() and returns the result', async () => {
            addressRepositoryMock.save.mockReturnValue(addressMock);
            expect(addressService.createOne).not.toHaveBeenCalled();
            const result = await addressService.createOne(req, createDto);
            expect(result).toEqual(addressMock);
        });
    });
});

这是我的服务类:

@Injectable()
export class AddressService extends TypeOrmCrudService<Address> {
    constructor(@InjectRepository(Address) repo) {
        super(repo);
    }
}

当测试运行时,这是发生的错误:

FAIL  src/modules/address/address.service.spec.ts
  ● AddressService › create Address › should calls addressRepository.save() and returns the result

    TypeError: Cannot read property 'columns' of undefined

       8 | export class AddressService extends TypeOrmCrudService<Address> {
       9 |  constructor(@InjectRepository(Address) repo) {
    > 10 |      super(repo);
         |      ^
      11 |  }
      12 | 
      13 |  public async findAddress(address: Address): Promise<Address> {

      at AddressService.onInitMapEntityColumns (../node_modules/@nestjsx/crud-typeorm/src/typeorm-crud.service.ts:327:45)
      at new TypeOrmCrudService (../node_modules/@nestjsx/crud-typeorm/src/typeorm-crud.service.ts:31:10)
      at new AddressService (modules/address/address.service.ts:10:3)
      at Injector.instantiateClass (../node_modules/@nestjs/core/injector/injector.js:276:19)
      at callback (../node_modules/@nestjs/core/injector/injector.js:74:41)
      at Injector.resolveConstructorParams (../node_modules/@nestjs/core/injector/injector.js:113:24)
      at Injector.loadInstance (../node_modules/@nestjs/core/injector/injector.js:78:9)
      at Injector.loadProvider (../node_modules/@nestjs/core/injector/injector.js:35:9)
          at async Promise.all (index 3)

  ● AddressService › create Address › should calls addressRepository.save() and returns the result

    TypeError: Cannot read property 'save' of undefined

      56 |  describe('create Address', () => {
      57 |      it('should calls addressRepository.save() and returns the result', async () => {
    > 58 |          addressRepositoryMock.save.mockReturnValue(addressMock);
         |                                ^
      59 |          expect(addressService.createOne).not.toHaveBeenCalled();
      60 |          const result = await addressService.createOne(req, createDto);
      61 |          expect(result).toEqual(addressMock);

      at Object.<anonymous> (modules/address/address.service.spec.ts:58:26)

Test Suites: 1 failed, 1 passed, 2 total

提前致谢!

Lorem ipsum dolor sit amet, consectetur adipiscing elit。Sed 坐在 amet nulla libero。塞德劳雷特。

4

2 回答 2

0

这就是我可以使模拟仓库与超类的构造函数一起工作的最小程度:

const mockRepo = {
  metadata: { columns: [], connection: { options: { type: '' } } }
}

我希望找到更好的选择:D

于 2020-07-17T08:09:21.810 回答
-1

因此,这与 CRUD 服务如何根据提供的存储库构建映射器有关。您需要让您的模拟存储库在具有要映射的 CRUD 列的实体上工作。

免责声明:我自己没有做过,但我已经阅读了有关它的源代码。

于 2019-11-19T18:43:20.917 回答