简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM