有谁知道使用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。塞德劳雷特。