Primeng确认服务的角度单位测试
我无法弄清楚如何在此功能中测试接受和拒绝功能:
confirmReplan(event: any) {
this.confirmationService.confirm({
target: event.target,
key: 'replan',
message: 'lorem',
icon: 'pi pi-exclamation-triangle',
accept: () => {
this.messageService.add(/** **/);
this.replanRequest();
this.displayReplan = false;
},
reject: (type: any) => {
this.rejectMessage(type);
}
});
}
查看类似的问题,我尝试了许多解决方案。似乎对他人有用的是这种风格:
let component: RequestsTableComponent;
let fixture: ComponentFixture<RequestsTableComponent>;
let confirmationServiceSpy: ConfirmationService;
let requestServiceSpy: RequestApiService;
let messageServiceSpy: MessageService;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [RequestsTableComponent],
imports: [HttpClientTestingModule],
providers: [MessageService, RequestApiService, ConfirmationService]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(RequestsTableComponent);
component = fixture.componentInstance;
fixture.detectChanges();
messageServiceSpy = TestBed.inject(MessageService);
requestServiceSpy = TestBed.inject(RequestApiService);
confirmationServiceSpy = TestBed.inject(ConfirmationService);
});
it('/**/', async() => {
spyOn(confirmationServiceSpy, 'confirm').and.callFake((confirmation: any) => {return confirmation.accept()});
component.confirmReplan({ target: undefined });
expect(confirmationServiceSpy.confirm).toHaveBeenCalled();
});
但是间谍从未被召唤。
jasmine coverage Reporter Reporter 代码覆盖范围报告
I cannot figure out how to test accept and reject functions inside this function:
confirmReplan(event: any) {
this.confirmationService.confirm({
target: event.target,
key: 'replan',
message: 'lorem',
icon: 'pi pi-exclamation-triangle',
accept: () => {
this.messageService.add(/** **/);
this.replanRequest();
this.displayReplan = false;
},
reject: (type: any) => {
this.rejectMessage(type);
}
});
}
Looking at similiar questions i tried many solutions. One that seems to work for others is of this style:
let component: RequestsTableComponent;
let fixture: ComponentFixture<RequestsTableComponent>;
let confirmationServiceSpy: ConfirmationService;
let requestServiceSpy: RequestApiService;
let messageServiceSpy: MessageService;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [RequestsTableComponent],
imports: [HttpClientTestingModule],
providers: [MessageService, RequestApiService, ConfirmationService]
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(RequestsTableComponent);
component = fixture.componentInstance;
fixture.detectChanges();
messageServiceSpy = TestBed.inject(MessageService);
requestServiceSpy = TestBed.inject(RequestApiService);
confirmationServiceSpy = TestBed.inject(ConfirmationService);
});
it('/**/', async() => {
spyOn(confirmationServiceSpy, 'confirm').and.callFake((confirmation: any) => {return confirmation.accept()});
component.confirmReplan({ target: undefined });
expect(confirmationServiceSpy.confirm).toHaveBeenCalled();
});
but the spy is never called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是对参数进行处理,并将
conve> Accept
和手动拒绝
。One way is to have a handle on the argument and call the
accept
andreject
manually.