简体   繁体   English

如何在测试中使用内部可观察方法进行覆盖

[英]How to make coverage with internal observable method in testing

here is the component methods:这是组件方法:

resetPage(): void {
        this.deletedClinicPosProps = { appId: [] };
        this.addedClinicPosProps = { appId: [] };
        this.resetHeaders();
        this.canSaveEnabled = { a: false, b: false, c: false };
        this.profileForm.hfsForm.reset();
        this.isDataSaved = false;
        this.setPage();
    }

and another method which calls the above method on post:以及另一种在发布时调用上述方法的方法:

postClinicUpdate() {
        this.postProfile();
        if (!this.profileForm.isHfsFormIsValid) {
            return;
        }
        const {
            slide,
            timezone: { codeId },
        } = this.profileFormValue;
        const { sjmAccDTO } = this.userProfile;
        const sjmAccountId = sjmAccDTO.sjmAccountId;

        const postData: UpdateClinicProps = {
            sjmAccountId: sjmAccountId,
            hfsStatus: slide === true ? 1 : 0,
            timeZoneCd: codeId,
            addedClinicIds: this.addedClinicPosProps.appId,
            deletedClinicIds: this.deletedClinicPosProps.appId,
            deletedCountryCds:
                this.profileForm.countrySelectionProps.removedCountries,
            addedCountryCds:
                this.profileForm.countrySelectionProps.addedCountries,
        };
        this.personnelViewDataService
            .updateClinics(postData)
            .subscribe((updates) => {
                if (updates) {
                    this.resetPage();
                    this.actOfSaveEnabled = false;
                    this._location.back();
                }
            });
    }

mock service for testing:用于测试的模拟服务:

const mockPersonnelViewDataService = {
        userProfileWithCodes$: of(testMockData),
        fetchUserProfileWithCode: function () {
            return this.userProfileWithCodes$;
        },
        updateClinics: function () {
            return { subscribe: () => {} };
        },
        responseUpdateClinic$: of({}),
    };

testing:测试:

it('should call postClinicUpdate to save data, and rest the page', fakeAsync(() => {
        const updateClinics = spyOn(service, 'updateClinics').and.callThrough();
        const postClinicUpdate = spyOn(
            component,
            'postClinicUpdate'
        ).and.callThrough();
        const resetPage = spyOn(component, 'resetPage').and.callThrough();
        const btnRemoveClinic = fixture.debugElement.query(
            By.css('#btn-removeClinic')
        ).nativeElement;
        const btnPostClinic = fixture.debugElement.query(
            By.css('#btn-postClinic')
        ).nativeElement;
        profileTable = fixture.debugElement.query(By.css('table#details-view'));
        const checkBox: HTMLInputElement = profileTable.query(
            By.css('tbody input[type=checkbox]')
        ).nativeElement;

        checkBox.click();
        tick(1000);

        btnRemoveClinic.click();
        tick(1000);
        btnPostClinic.click();
        expect(postClinicUpdate).toHaveBeenCalled();
        tick(1000);
        component.resetPage();
        expect(resetPage).toHaveBeenCalled();
    }));

no error thrown.没有抛出错误。 all are success.都是成功的。 But my resetPage() method not under the test coverage.但是我的resetPage()方法不在测试范围内。 still it show under uncover part.它仍然显示在未覆盖的部分下。 How to make my resetPage come under coverage with current testing?如何使我的resetPage受到当前测试的覆盖?

what I missed or what change need i do?我错过了什么或我需要做什么改变? thanks in advance提前致谢

  • You are directly calling component.resetPage();您直接调用component.resetPage(); that is probably why the test case is passing.这可能就是测试用例通过的原因。
  • updateClinics returns an object not an observable updateClinics 返回 object 不是可观察的

Remove component.resetPage() and add return of('test') to the mock删除component.resetPage()并将return of('test')添加到 mock

const mockPersonnelViewDataService = {
  updateClinics: function () {
    return of('test');
  },

 ...
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM