[英]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. 对于为什么我undefined
为我正在测试的函数的返回值,我有些困惑。
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);
我期望console.log(mainScope.entityItemsIntoObjectChecked(dummyEntityArray))
行基于我先前在mainScope.entityItemsIntoObjectChecked(dummyEntityArray);
行中传递的数组dummyEntityArray
输出我的数组mainScope.entityItemsIntoObjectChecked(dummyEntityArray);
but I get undefined
on console.log. 但我在console.log上undefined
。 Why is this? 为什么是这样?
Your $scope.entityItemsIntoObjectChecked
method does not return anything so the return value will be undefined
. 您的$scope.entityItemsIntoObjectChecked
方法不会返回任何内容,因此返回值将是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. 您可以从方法返回newEntityWithChecked
数组,也可以监视shareDataService.setEntityArray
方法以验证是否传递了预期的数组。
You can read the documentation on spies here: http://jasmine.github.io/2.0/introduction.html#section-Spies 您可以在此处阅读有关间谍的文档: http : //jasmine.github.io/2.0/introduction.html#section-Spies
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.