繁体   English   中英

Karma angularjs方法测试中的console.log

[英]console.log in karma angularjs method testing

对于为什么我undefined为我正在测试的函数的返回值,我有些困惑。

这是我正在测试的功能:

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

和我的测试套件的一部分:

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

我期望console.log(mainScope.entityItemsIntoObjectChecked(dummyEntityArray))行基于我先前在mainScope.entityItemsIntoObjectChecked(dummyEntityArray);行中传递的数组dummyEntityArray输出我的数组mainScope.entityItemsIntoObjectChecked(dummyEntityArray);

但我在console.log上undefined 为什么是这样?

您的$scope.entityItemsIntoObjectChecked方法不会返回任何内容,因此返回值将是undefined

您可以从方法返回newEntityWithChecked数组,也可以监视shareDataService.setEntityArray方法以验证是否传递了预期的数组。

您可以在此处阅读有关间谍的文档: http : //jasmine.github.io/2.0/introduction.html#section-Spies

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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