Javascript 茉莉花测试,.not.tohavebeencall();

Javascript 茉莉花测试,.not.tohavebeencall();,javascript,unit-testing,jasmine,code-coverage,istanbul,Javascript,Unit Testing,Jasmine,Code Coverage,Istanbul,我正在用Jasmine和Instanbul进行单元测试。我正在测试提交表单。我已经为它做了几个单元测试。但现在我想知道,如果: this.allowSubmitAgain = false; this method: this.diplomaService.saveDiplomaMetaData(false, this.resources.opslaanSpinnerTekst).pipe(map(() => { 不会被执行。因为在instanbul的这一行: if (this.allo

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

this.allowSubmitAgain = false; this method:

this.diplomaService.saveDiplomaMetaData(false, this.resources.opslaanSpinnerTekst).pipe(map(() => {
不会被执行。因为在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.
Expected spy saveDiplomaMetaData not to have been called.
那么如何解决这个问题呢

多谢各位

我现在是这样的:

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();

    }));
 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();
    }));
我也不会出错

但是在伊斯坦布尔的代码报道中。“未采用其他路径”仍然可见

Eif(this.allowSubmitAgain){

所以,在这一行,它仍然说,其他路径没有采取

如果我这样做:

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();

    }));
 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();
    }));
我得到这个错误:

TypeError: Cannot read property 'subscribe' of undefined.
Expected spy saveDiplomaMetaData not to have been called.

但是我必须做什么呢?

您可以尝试通过将
可观察的
添加到
spyOn(外交官服务模拟,'保存外交官元数据')来消除此错误吗
首先?这样我们就可以断言测试用例有什么问题了。谢谢你的回复。我更新了帖子。你新更新的测试用例中没有任何操作。你能更新它并与我们分享测试用例结果吗?你的确切意思是什么?我用代码覆盖率更新了帖子