簡體   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