3

I am doing unit testing with Jasmine, and Instanbul. I am testing a submit form. And I have already several unit tests for it. But now I want that if:

this.allowSubmitAgain = false; this method:

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

not will be executed. Because on this line in instanbul:

if (this.allowSubmitAgain) {

it says E: else path not taken.

This is the whole method:

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

        }
    }

And this is my unit test:

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


    }));

But I get this error:

TypeError: Cannot read property 'subscribe' of undefined.

So how to fix that?

Thank you

I have it now like this:

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

    }));

And I don't get errors.

But in the code coverage of Istanbul. The 'else path not taken' is still visible.

Eif (this.allowSubmitAgain) {

So on this line it still says else path not taken.

if I do it like this:

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

I get this error:

Expected spy saveDiplomaMetaData not to have been called.

But what I have to do ?

niceToExist
  • 55
  • 1
  • 10
  • Could you try eliminating this error by adding `Observable` to the `spyOn(diplomaServiceMock, 'saveDiplomaMetaData')` first? So we can assert what's wrong with the test cases. – mixth Jan 31 '19 at 07:40
  • Thank you for your reply. I updated the post. – niceToExist Jan 31 '19 at 08:24
  • There is no action in your newly updated test case. Could you update it and share us the test case result? – mixth Jan 31 '19 at 08:26
  • What exactly do you mean? – niceToExist Jan 31 '19 at 08:29
  • I updated the post with the code coverage – niceToExist Jan 31 '19 at 08:32
  • The test case you have just updated (after `I have it now like this:`) does not include any actions like: `component.diplomaDataSubmitted(); fixture.detectChanges();` in the first case. ---- The error that you've got before (`subscribe`) indicated that the unit under test did call the line `this.diplomaService.saveDiplomaMetaData`. That was why I wanted to see how the assert parts would report after adding correct `Observable` return. – mixth Jan 31 '19 at 08:41
  • I updated the post. But what I have to do? – niceToExist Jan 31 '19 at 08:49
  • The test case failed because `saveDiplomaMetaData` is called, but it shouldn't, right? Then let try to arrange it by adding `component.allowSubmitAgain = false;` before any actions. – mixth Jan 31 '19 at 09:17
  • Yes that was the case! Thank you – niceToExist Jan 31 '19 at 09:54
  • So solved! Thank you. ok – niceToExist Jan 31 '19 at 09:55
  • You're welcome :) – mixth Jan 31 '19 at 09:55

0 Answers0