
[英]Nest.js Jest cannot mock testing app but can mock testing controller
[英]Nest Js Jest Unit Testing: Providing a mock implementation not used from controller
作为我之前工作的一部分,我已经实现了一个模拟 class,我想将其作为 Jest 单元测试的一部分提供。
让我用代码解释,这样更好:
我的 Controller 代码:
export class VpcController {
constructor(private readonly vpcService: VpcService) {}
@Get
list() {
return this.vpcService.list();
}
}
我的 controller Jest 单元测试:
class VpcServiceMockFactory {
private list() {
return jest.fn().mockResolvedValue([TEMPLATE_VPC]);
}
getMock() {
const repoService: RepositoryService = new RepositoryService();
const vpsServiceMock: VpcServiceMock = new VpcServiceMock(repoService);
vpsServiceMock.create(TEMPLATE_VPC.name);
// This works
// return {
// list: this.list(),
// }
// This does not work
return {
list: vpsServiceMock.list(),
get: vpsServiceMock.get(TEMPLATE_VPC.id),
// create: vpsServiceMock.create('new-vpc'),
// update: vpsServiceMock.update(TEMPLATE_VPC.id, 'updated-name'),
// delete: vpsServiceMock.delete(TEMPLATE_VPC.id),
}
}
}
describe('VpcControllerTest', () => {
let controller: VpcController;
let spyService: VpcService;
beforeEach(async () => {
// Mock Services
const MockVpcServiceProvider = {
provide: VpcService,
useFactory: () => new VpcServiceMockFactory().getMock()
}
// Class-unter-test instantiation
const module: TestingModule = await Test.createTestingModule({
controllers: [VpcController],
providers: [VpcService, MockVpcServiceProvider],
}).compile()
// Get the instance handlers
controller = module.get<VpcController>(VpcController);
spyService = module.get<VpcService>(VpcService);
});
it('Get collection of VPCs', async () => {
// Execute the method
const result = await controller.list();
// Assertion
expect(spyService.list).toHaveBeenCalled();
expect(result.length).toBe(1);
expect(result[0].name).toBe('zopa');
});
}
我的 VpcServiceMock class:
export class VpcServiceMock {
constructor(private repository: RepositoryService) {}
list() {
return this.repository.list<VpcModel>(VPC);
}
}
我的存储服务 class:
async list<T>(type: VPC): Promise<T[]> {
return <T[]>this.aSimpleJsonObject[type];
}
但是,当我运行它时,它显示此错误:
● VpcControllerTest › Test-2: Get collection of VPCs
TypeError: this.vpcService.list is not a function
38 | @ApiForbiddenResponse({ description: 'Unauthorized Request' })
39 | list() {
> 40 | return this.vpcService.list();
| ^
41 | }
42 |
43 | @Get(':id')
at VpcController.list (src/vpc/vpc.controller.ts:40:28)
at Object.<anonymous> (src/vpc/vpc.controller.spec.ts:87:37)
所以我能让它工作的唯一方法是:
如果我在 Jest 单元测试的VpcServiceMockFactory
class 中提供模拟实现(就像我在注释掉的代码中显示的// This works
)。
我肯定在这里遗漏了一些我无法弄清楚的东西。
得到我的问题。
基本上我错过了jest.fn().mockResolvedValue
。
修改以使其工作:
class VpcServiceMockFactory {
async getMock() {
const repoService: RepositoryService = new RepositoryService();
const vpcServiceMock: VpcServiceMock = new VpcServiceMock(repoService);
await vpcServiceMock.create(TEMPLATE_VPC.name); // repository store initialization
return {
list: jest.fn().mockResolvedValue(vpcServiceMock.list()),
get: jest.fn().mockResolvedValue(vpcServiceMock.get(TEMPLATE_VPC.id)),
create: jest.fn().mockResolvedValue(vpcServiceMock.create('new-vpc')),
update: jest.fn().mockResolvedValue(vpcServiceMock.update(TEMPLATE_VPC.id, 'updated-name')),
delete: jest.fn().mockResolvedValue(vpcServiceMock.delete(TEMPLATE_VPC.id)),
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.