控制台.log在Karma AngularJS方法测试中

console.log in karma angularjs method testing

本文关键字:方法 测试 AngularJS Karma log 控制台      更新时间:2023-09-26

我有点困惑为什么我要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输出我的数组

但我在控制台上得到了undefined.log。 这是为什么呢?

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

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

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