Javascript 用于启动确认服务的Jasmine测试用例不工作
我有一个函数,它在PrimeNg确认服务的“accept”调用中执行一些操作。我尝试为它编写一个单元测试用例,如下所示:Javascript 用于启动确认服务的Jasmine测试用例不工作,javascript,angular,jasmine,karma-jasmine,spyon,Javascript,Angular,Jasmine,Karma Jasmine,Spyon,我有一个函数,它在PrimeNg确认服务的“accept”调用中执行一些操作。我尝试为它编写一个单元测试用例,如下所示: fit('submit preview config', fakeAsync(() => { addValues(); component.submitConfig(); component.submitPreviewForm(); fixture.detectChanges(); const confirmationService =
fit('submit preview config', fakeAsync(() => {
addValues();
component.submitConfig();
component.submitPreviewForm();
fixture.detectChanges();
const confirmationService = TestBed.get(ConfirmationService);
tick(200);
spyOn<any>(confirmationService, 'confirm').and.callFake((params: any) => {
params.accept();
httpMock.expectOne(baseUrl + '/api/project/addOrUpdate').flush(mockSubmitResponse);
expect(component.successMsz).toBe(mockSubmitResponse.message);
});
flush();
}));
fit('submit preview config',fakeAsync(()=>{
addValues();
component.submitConfig();
submitPreviewForm();
fixture.detectChanges();
const confirmationService=TestBed.get(confirmationService);
勾选(200);
spyOn您的操作顺序似乎有点混乱,在调用submitPreviewform
之前,您需要spy
试试这个:
fit('submit preview config', fakeAsync(() => {
const confirmationService = TestBed.get(ConfirmationService); // grab a handle of confirmationService
spyOn<any>(confirmationService, 'confirm').and.callFake((params: any) => {
params.accept();
httpMock.expectOne(baseUrl + '/api/project/addOrUpdate').flush(mockSubmitResponse);
expect(component.successMsz).toBe(mockSubmitResponse.message);
}); // spy on confirmationService.confirm now
addValues();
component.submitConfig();
component.submitPreviewForm();
fixture.detectChanges();
tick(200);
flush();
}));
fit('submit preview config',fakeAsync(()=>{
const confirmationService=TestBed.get(confirmationService);//获取confirmationService的句柄
spyOn(确认服务,'confirmationService')。和.callFake((参数:any)=>{
参数accept();
httpMock.expectOne(baseUrl+'/api/project/addOrUpdate').flush(mockSubmitResponse);
expect(component.successsz).toBe(mockSubmitResponse.message);
});//监视确认服务。立即确认
addValues();
component.submitConfig();
submitPreviewForm();
fixture.detectChanges();
勾选(200);
冲洗();
}));
您的操作顺序似乎有点混乱,在调用submitPreviewform
之前,您需要spy
试试这个:
fit('submit preview config', fakeAsync(() => {
const confirmationService = TestBed.get(ConfirmationService); // grab a handle of confirmationService
spyOn<any>(confirmationService, 'confirm').and.callFake((params: any) => {
params.accept();
httpMock.expectOne(baseUrl + '/api/project/addOrUpdate').flush(mockSubmitResponse);
expect(component.successMsz).toBe(mockSubmitResponse.message);
}); // spy on confirmationService.confirm now
addValues();
component.submitConfig();
component.submitPreviewForm();
fixture.detectChanges();
tick(200);
flush();
}));
fit('submit preview config',fakeAsync(()=>{
const confirmationService=TestBed.get(confirmationService);//获取confirmationService的句柄
spyOn(确认服务,'confirmationService')。和.callFake((参数:any)=>{
参数accept();
httpMock.expectOne(baseUrl+'/api/project/addOrUpdate').flush(mockSubmitResponse);
expect(component.successsz).toBe(mockSubmitResponse.message);
});//监视确认服务。立即确认
addValues();
component.submitConfig();
submitPreviewForm();
fixture.detectChanges();
勾选(200);
冲洗();
}));