首页 > 解决方案 > 茉莉花测试,.not.toHaveBeenCalled();

问题描述

我正在用 Jasmine 和 Instanbul 进行单元测试。我正在测试提交表单。我已经有几个单元测试了。但现在我想要,如果:

this.allowSubmitAgain = false; this method:

this.diplomaService.saveDiplomaMetaData(false, this.resources.opslaanSpinnerTekst).pipe(map(() => {

not 将被执行。因为在 instanbul 的这条线上:

if (this.allowSubmitAgain) {

上面写着 E:否则没有采取路径。

这是整个方法:

diplomaDataSubmitted() {
        if (this.formModel && this.formModel.form.valid) {

            this.formService.formStoreService.hideValidationSummary(this.formName);
            Eif (this.allowSubmitAgain) {
                this.allowSubmitAgain = false;
                this.diplomaService.saveDiplomaMetaData(false, this.resources.opslaanSpinnerTekst).pipe(map(() => {
                    const documents: DocumentModel[] = this.formModel.getAngularFormControl(DiplomaFormKeysModel.keys.diplomasUpload).value.files;

                    this.diplomaService.saveDocumentForDiploma(documents, () => {
                        this.router.navigateByUrl(RoutesRegisterModel.pathnameMyRegisterRouteDiplomas).then(() => {
                            this.feedbackService.addSuccessMessageOnMainPortal(
                                {
                                    key: this.resourceKeys.succesvolWijziging,
                                    value: this.resources.succesvolWijziging,
                                }
                            );
                        });
                    }, (error: any) => {
                        this.allowSubmitAgain = true;
                        observableThrowError(error);
                    }, this.resources, this.resourceKeys);
                })).subscribe(() => { }, (error: any) => {
                    this.allowSubmitAgain = true;
                    observableThrowError(error);
                });
            }
        } else {
            this.formService.formStoreService.showValidationSummary(this.formName);

        }
    }

这是我的单元测试:

it('should not allow submit beaucse form has errors', fakeAsync(() => {

        // Arrange
        const spySaveMetaDataDiploma = spyOn(diplomaServiceMock, 'saveDiplomaMetaData');

        diplomaServiceMock.returnErrorResponse();
        // Act
        component.diplomaDataSubmitted();
        fixture.detectChanges();

        // Assert
         expect(spySaveMetaDataDiploma).not.toHaveBeenCalled();
        console.log('testcase');
        expect(component.allowSubmitAgain).toBe(false);


    }));

但我得到这个错误:

TypeError: Cannot read property 'subscribe' of undefined.

那么如何解决呢?

谢谢

我现在有这样的:

it('should not allow submit beaucse form has errors', fakeAsync(() => {

        // Arrange

        const spySaveDiploma = spyOn(diplomaServiceMock, 'saveDiplomaMetaData').and.callThrough();

        // Act
        component.allowSubmitAgain = false;

        // Assert

        console.log('testcase');
        expect(component.allowSubmitAgain).toBe(false);
        expect(spySaveDiploma).not.toHaveBeenCalled();

    }));

而且我没有错误。

但在伊斯坦布尔的代码覆盖范围内。“未采取的其他路径”仍然可见。

Eif (this.allowSubmitAgain) {

所以在这条线上它仍然说没有采取其他路径。

如果我这样做:

 it('should not allow submit again beaucse form has errors', fakeAsync(() => {

        const spySaveDiploma = spyOn(diplomaServiceMock, 'saveDiplomaMetaData').and.callThrough();

        component.diplomaDataSubmitted();
        component.allowSubmitAgain = false;
        fixture.detectChanges();

        console.log('testcase');
        expect(component.allowSubmitAgain).toBe(false);
        expect(spySaveDiploma).not.toHaveBeenCalled();
    }));

我收到此错误:

Expected spy saveDiplomaMetaData not to have been called.

但我必须做什么?

标签: javascriptunit-testingjasminecode-coverageistanbul

解决方案


推荐阅读