Angular 测试覆盖率未能覆盖测试条件
我用Jasmine编写了一个Angular 8测试。目前,to code coverage report投诉未涵盖以下情况。我写了一个测试,我认为下面的测试涵盖了测试内容,但似乎没有涵盖它。标记为fit的测试是我为满足该条件而执行的测试。有什么问题吗 确认方法中不包括以下内容和条件: 和&this.permissions.ViewNotesAngular 测试覆盖率未能覆盖测试条件,angular,jasmine,Angular,Jasmine,我用Jasmine编写了一个Angular 8测试。目前,to code coverage report投诉未涵盖以下情况。我写了一个测试,我认为下面的测试涵盖了测试内容,但似乎没有涵盖它。标记为fit的测试是我为满足该条件而执行的测试。有什么问题吗 确认方法中不包括以下内容和条件: 和&this.permissions.ViewNotes if (!this.message && ((type === 'Approve' && this.permis
if (!this.message
&& ((type === 'Approve' && this.permissions.ViewNotes)
|| (type !== 'Approve'))) {
this.messageService.add('Message is required.', 'warning');
return;
}
测试组件
describe('ApproveComponent', () => {
let component: ApproveComponent;
let injector: TestBed;
let fixture: ComponentFixture<ApproveComponent>;
const mockService: ApprovalsService = <ApprovalsService>{
approve: (id: string, type: string, message: string) => <Promise<any>>{},
reset: (id: string, type: string, message: string) => <Promise<any>>{},
reject: (id: string, type: string, message: string) => <Promise<any>>{},
get: (type: string, id: string) => <Promise<any>>{},
};
const mockRoute = { params: of({ id: '123', type: 'test' }), snapshot: {} };
function setupComponent(getResult: any = {}) {
spyOn(mockService, nameof<ApprovalsService>('approve')).and.returnValue(Promise.resolve({}));
spyOn(mockService, nameof<ApprovalsService>('reset')).and.returnValue(Promise.resolve({}));
spyOn(mockService, nameof<ApprovalsService>('reject')).and.returnValue(Promise.resolve({}));
spyOn(mockService, nameof<ApprovalsService>('get')).and.returnValue(Promise.resolve(getResult));
TestBed.configureTestingModule({
imports: [
DxTextAreaModule,
DxButtonModule,
SharedModule,
RouterTestingModule.withRoutes([{ path: 'approvals', component: ApproveComponent }])
],
declarations: [ApproveComponent],
providers: [
{ provide: ApprovalsService, useValue: mockService },
{ provide: ActivatedRoute, useValue: mockRoute },
{ provide: MessageService, useClass: MockMessageService },
{ provide: ConfirmationDialogService, useValue: ConfirmationDialogServiceMock },
{ provide: NgxPermissionsService, useClass: MockNgxPermissionsService }
]
})
.compileComponents();
fixture = TestBed.createComponent(ApproveComponent);
injector = getTestBed();
component = fixture.componentInstance;
spyOn((<any>component).router, 'navigate').and.returnValue(true);
fixture.detectChanges();
}
it('should create and call get', () => {
setupComponent();
expect(component).toBeTruthy();
expect(mockService.get).toHaveBeenCalled();
});
it('should call get and do not set body on empty result', () => {
setupComponent(null);
expect(component).toBeTruthy();
expect(component.body).toBe('Loading content for approval...');
});
it('should call confirmation dialog when accept confirmation is called', () => {
setupComponent();
const dialogService = injector.get(ConfirmationDialogService);
const dialogServiceSpy = spyOn(dialogService, 'show').and.callThrough();
component.showMessage = true;
component.message = 'Approved because potato';
fixture.ngZone.run(() => component.confirmation('Approve'));
expect(dialogServiceSpy).toHaveBeenCalled();
expect(dialogServiceSpy).toHaveBeenCalled();
});
it('should show the comments box when accept confirmation is called with compliance role', () => {
setupComponent();
var mockNgxPermissionsService = new MockNgxPermissionsService();
component.permissions = mockNgxPermissionsService.getPermissions();
component.message = 'Approved because potato';
fixture.ngZone.run(() => component.confirmation('Approve'));
expect(component.showMessage).toBe(true);
});
fit('should set ViewNotes permission when accept confirmation is called with compliance role', () => {
setupComponent();
var mockNgxPermissionsService = new MockNgxPermissionsService();
component.permissions = mockNgxPermissionsService.getPermissions();
component.message = 'Approved because potato';
fixture.ngZone.run(() => component.confirmation('Approve'));
expect(component.permissions.ViewNotes).not.toBeNull;
});
});
在等级库文件中有以下内容:
const mockRoute = { params: of({ id: '123', type: 'test' }), snapshot: {} };
您必须将类型更改为“批准”,以便测试和&this.permissions.ViewNotes场景。它没有覆盖,因为您使用了
“&&”运算符,因此在不检查第二个条件的情况下,它将转到下一个语句。我认为您可以创建另一个来描述和实现此场景。I set component.showMessage=true;但这并没有解决我发现的问题是this.message没有设置为null。只有当它为空时,条件才被转换。如果this.message不是空的,那么它将不会覆盖任何这些条件,因为您正在使用“and”运算符。
const mockRoute = { params: of({ id: '123', type: 'test' }), snapshot: {} };