Angularjs:jasmine因果报应声明(如果没有选择其他路径)
我如何在单元测试用例覆盖率中摆脱这些语句(如果没有采取其他路径) 这是我的规范文件:Angularjs:jasmine因果报应声明(如果没有选择其他路径),angularjs,unit-testing,karma-jasmine,test-coverage,Angularjs,Unit Testing,Karma Jasmine,Test Coverage,我如何在单元测试用例覆盖率中摆脱这些语句(如果没有采取其他路径) 这是我的规范文件: describe('Add Mode', () => { beforeEach(() => { TestBed.configureTestingModule({ imports: [ ReactiveFormsModule, FormsModule, Sha
describe('Add Mode', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
ReactiveFormsModule,
FormsModule,
SharedModule,
BootstrapModalModule,
NgbModule.forRoot(),
HttpClientModule
],
declarations: [
AssetAddComponent,
FileUploadComponent
],
providers: [
{ provide: ActivatedRoute, useValue: mockActivatedRouteAddMode },
{ provide: Router, useValue: mockRouter },
Overlay,
OverlayRenderer,
SiteService,
AuthHttp,
MessageService,
FileUploadService,
LocalCacheService,
LocalStorageService
]
});
fixture = TestBed.createComponent(AssetAddComponent);
component = fixture.componentInstance;
siteService = TestBed.get(SiteService);
spyOn(siteService, 'getSiteAssetById')
.and.returnValue(Observable.of(objSiteAssetDetailById))
spyOn(siteService, 'getSiteLocales')
.and.returnValue(Observable.of(objSiteLocale));
component.ngOnInit();
assetForm = component.assetForm;
});
it('should have a defined component', () => {
expect(component).toBeDefined();
});
it('form should be invalid when in add mode', () => {
expect(component.assetForm.valid).toBeFalsy();
});
it('submitting asset form', () => {
assetForm.controls['title'].setValue('Test Asset');
assetForm.controls['description'].setValue('This is description for Test Asset.');
expect(assetForm.valid).toBeTruthy();
spyOn(siteService, 'addSiteAsset')
.and.returnValue(Observable.of(objAddSiteAssetResponse));
component.saveAsset();
});
it('updating asset form', () => {
assetForm.controls['title'].setValue('Test Asset ew');
assetForm.controls['description'].setValue('This is description for Test Asset.');
expect(assetForm.valid).toBeTruthy();
spyOn(siteService, 'updateSiteAsset')
.and.returnValue(Observable.of(objUpdateSiteAssetResponse));
component.updateAsset();
});
it('should delete the asset', () => {
spyOn(siteService, 'deleteSiteAsset')
.and.returnValue(Observable.of(objSiteAssetDetailById));
component.deleteAsset(objSiteAssetDetailById);
fixture.detectChanges();
expect(Response).toBeTruthy();
});
it('should publish the asset', () => {
spyOn(siteService, 'updateSiteAsset')
.and.returnValue(Observable.of(objSiteAssetDetailById));
component.publishAsset(publish);
fixture.detectChanges();
expect(Response).toBeTruthy();
});
it('should unpublish the asset', () => {
spyOn(siteService, 'updateSiteAsset')
.and.returnValue(Observable.of(objSiteAssetDetailById));
component.publishAsset(isUndefined);
fixture.detectChanges();
expect(Response).toBeTruthy();
});
it('should cancelAsset', () => {
component.cancelAsset();
expect(component.isEditMode).toBe(true);
});
// it('should cancelAsset', () => {
// component.cancelAsset();
// expect(component.isEditMode).toBe(false);
// });
// it('should test onFileUpload', () => {
// component.onFileUpload();
// expect(component.uploadedFiles).toBeDefined();
// });
it('should test onFileUpload', () => {
component.onFileUpload();
expect(component.isLocalise).toBe(false);
});
it('should test getSafeUrl', () => {
component.updatePublishedLocales('');
expect(component.publishLocales).toBeTruthy();
});
it('should test changeAssetType', () => {
component.changeAssetType();
expect(component.isLocalise).toBe(false);
expect(localiseFormData.title).toEqual('testing');
});
it('should test changeAssetType', () => {
component.changeAssetTypeOfFile();
expect(component.localesForAsset).toBeTruthy();
});
it('should test getAsset', () => {
component.getAsset('');
expect(component.hasData).toBe(false);
});
it('should test selectDataLocale', () => {
component.selectDataLocale();
expect(component.titleLocales).toBeTruthy();
});
it('should test selectAssetLocale', () => {
component.selectAssetLocale();
expect(component.assetLocales).toBeTruthy();
});
it('should test updatePublishedLocales', () => {
component.updatePublishedLocales('');
expect(component.publishLocales).toBeTruthy();
});
it('should test checkPublishingLocaleStatus', () => {
component.checkPublishingLocaleStatus('data', 'action');
const localeArray = []
expect(component.localeArray.push(name, status)).toBeTruthy();
return localeArray;
});
});
我试图测试并覆盖if/else语句,但代码覆盖率报告显示if-path-not-take和else-path-not-take
我是单元测试用例覆盖率方面的新手,有谁能帮助我如何覆盖这些语句。这些消息中的每一条都表明您对其中一条路径没有测试条件。尝试编写一个调用函数的测试,并为每个if/else条件传入解析falsy(或truthy)的值。一次一条地记下每一条信息,并编写相应的测试。我认为你没有仔细阅读他的问题。请注意,测试中没有“else”块他说:"因此,保险范围不应扩大complaining@pixelbits我确实仔细研究了他的问题。缺少
else
块不是重点。如果存在If
块,则根据条件是否返回truthy,隐式存在多个路径。无论是否存在else
块,它都会抱怨,除非您涵盖这两种情况(如果块运行或不运行)。我明白您的意思。我遇到了一个类似的问题。我有一个if块,测试明确地覆盖了分支。然而,没有其他块,但它仍然抱怨分支机构没有被覆盖。。。不确定是否有其他人遇到此问题。@pixelbits确保您的测试包括if条件为truthy和falsy。这应该可以解决投诉。