Jdore
Jdore

Reputation: 43

why test coverage is not 100%

I wrote a method which returns true if there are folders left in the modal to be added and return false if there are no folders left in the modal to be added to the application

my method in angular/typesrcipt

    ifAllFoldersHaveBeenAdded() {
        let added = false;
        let folderToExclude = find(this.bdDataModel.lenderDefinedFolders, { 
 name: 'Correspondence' });
        let foldersToBeChecked = without(this.bdDataModel.lenderDefinedFolders, folderToExclude);

        for (let i = 0; i < foldersToBeChecked.length; i++) {
            let folderPresent = find(this.bdDataModel.folders, { name: foldersToBeChecked[i].name });
            if (folderPresent) {
                added = true;

            } else {
                added = false;
                return added;
            }

        }
        return added;
    }

my test...

describe('ifAllFoldersHaveBeenAdded()', () => {
            it('returns false if there are folders left in the modal to be added', () => {
                let added = false;
                $ctrl.bdDataModel.folders;
                $ctrl.foldersToBeChecked = [{ name: 'FMS' }, { name: 'Other' }];
                $ctrl.folderPresent = { name: 'FMS' };

                $ctrl.ifAllFoldersHaveBeenAdded();
                added = true;
                expect(added).toEqual(true);

            });

            it('returns true is all folders have been added to the application', () => {
                let added = false;
                $ctrl.bdDataModel.folders;
                $ctrl.foldersToBeChecked = [{ name: 'FMS' }, { name: 'Other' }];
                $ctrl.folderPresent = { name: 'LMI' };

                $ctrl.ifAllFoldersHaveBeenAdded();
                expect(added).toEqual(false);
            });

        });

Both tests pass but icov coverage report is marking the following lines as uncovered...

find(this.bdDataModel.folders, { name: foldersToBeChecked[i].name });
            if (folderPresent) {
                added = true;
            }
            else {
                added = false;
                return added;
            }

please tell what i should add in the test to have a 100% test coverage for this unit test. thanks in advance

Upvotes: 0

Views: 81

Answers (1)

tehbeardedone
tehbeardedone

Reputation: 2858

There is so much wrong with your test. First off, you need to get rid of the local added variables completely. Doing something like the following

added = true;
expect(added).toEqual(true);

Is essentially the same as doing expect(true).toBe(true)

Next, the folderPresent and foldersToBeChecked variables are local to the ifAllFoldersHaveBeenAdded function. They are not controller variables. You don't need to set $ctrl.folderPresent or $ctrl.foldersToBeChecked in your test.

You need to define these two variables $ctrl.bdDataModel.lenderDefinedFolders and $ctrl.bdDataModel.folders

Because lenderDefinedFolders is undefined the length of foldersToBeChecked in your function is going to be 0. So the for loop will never execute.

Your test should look something like this:

it('returns false if there are folders left in the modal to be added', () => {
   $ctrl.bdDataModel.lenderDefinedFolders = [{ //add lender defined folders here }]
   $ctrl.bdDataModel.folders = [{ //add some folders here}]

   let added = $ctrl.ifAllFoldersHaveBeenAdded();
   expect(added).toEqual(true);
});

Upvotes: 1

Related Questions