Angularjs:jasmine-karma语句(如果没有采用其他路径)

时间:2018-03-27 10:12:16

标签: angularjs unit-testing karma-jasmine test-coverage

enter image description here

如何在单元测试用例覆盖中摆脱这些语句(如果没有采用其他路径)。

这是我的spec文件:

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但是代码覆盖率报告显示路径是否未被采用而其他路径未被采用。

我是单元测试案例报道的新手,任何人都可以帮助我如何报道这些陈述。

1 个答案:

答案 0 :(得分:0)

每条消息都表明您没有针对其中一条路径的测试条件。尝试编写一个调用函数并传递值的测试,以解决每种if / else条件的错误(或正确)。一次获取每条消息,并编写适当的测试来做到这一点。