茉莉花测试,.not.toHaveBeenCalled();

时间:2019-01-31 03:32:11

标签: javascript unit-testing jasmine code-coverage istanbul

我正在与Jasmine和Instanbul进行单元测试。我正在测试提交表单。我已经有几个单元测试了。但是现在我想要的是:

this.allowSubmitAgain = false; this method:

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

不被执行。因为在伊斯坦布尔这条线上:

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.

但是我该怎么办?

0 个答案:

没有答案