notAChance
notAChance

Reputation: 1430

console.log in karma angularjs method testing

I'm a little confused as to why I am getting undefined as a return value for a function I am testing.

here is the function I'm testing :

 $scope.entityItemsIntoObjectChecked = function (entityItems) {
    newEntityWithChecked = entityItems.map(function (value) {
        return { "val": value, "checked": false }; 
    });
    shareDataService.setEntityArray(newEntityWithChecked);
}

and part of my test suite :

it("should arrange an array into ovject with extra value for entities + depots", inject(function (shareDataService) {
        var dummyEntityArray = ["ent1", "ent2", "ent3"];
        var expectedObjectArray = [{ val: "ent1", checked: false }, { val: 'ent2', checked: false }, { val: 'ent3', checked: false }];

        expect(mainScope.entityItemsIntoObjectChecked).toBeDefined();
        mainScope.entityItemsIntoObjectChecked(dummyEntityArray);
        console.log(mainScope.entityItemsIntoObjectChecked(dummyEntityArray))
}))

I'm expecting the line console.log(mainScope.entityItemsIntoObjectChecked(dummyEntityArray)) to output my array based on the array dummyEntityArray which I have passed in previously on the line mainScope.entityItemsIntoObjectChecked(dummyEntityArray);

but I get undefined on console.log. Why is this?

Upvotes: 0

Views: 625

Answers (1)

Saravana
Saravana

Reputation: 40554

Your $scope.entityItemsIntoObjectChecked method does not return anything so the return value will be undefined.

You can either return the newEntityWithChecked array from the method or spy on your shareDataService.setEntityArray method to verify that the expected array is being passed.

You can read the documentation on spies here: http://jasmine.github.io/2.0/introduction.html#section-Spies

Upvotes: 1

Related Questions