sessionStorage ไม่ได้กำหนดไว้ใน Jasmine

ฉันติดตามโพสต์นี้เพื่อตั้งค่า sessionStorage ของฉัน:

แต่ไม่ว่าฉันจะทำอะไร sessionStorage ของฉันก็ยังไม่ได้กำหนดไว้ ฉันสามารถก้าวเข้าสู่ storageMock() ได้ แต่เมื่อฉันก้าวออกไป มันยังคงไม่ได้กำหนดไว้ ฉันทำผิดอะไร?

นี่คือรหัส:

/// <reference path="../angular.js" />
/// <reference path="../angular-mocks.js" />
/// <reference path="../angular-animate.js" />
/// <reference path="../angular-ui/ui-bootstrap.js" />
/// <reference path="../angular-ui/ui-bootstrap-tpls.js" />

/// <reference path="../../../bluescopebuildings/app/app.js" />
/// <reference path="../../../bluescopebuildings/app/applicationapp.js" />


describe('Controller: applicationController', function () {
    // Storage Mock
    function storageMock() {
        var storage = {};

        return {
            setItem: function (key, value) {
                storage[key] = value || '';
            },
            getItem: function (key) {
                return storage[key] || null;
            },
            removeItem: function (key) {
                delete storage[key];
            },
            get length() {
                return Object.keys(storage).length;
            },
            key: function (i) {
                var keys = Object.keys(storage);
                return keys[i] || null;
            }
        };
    };
    var applicationCtrl, $scope, $window;


    beforeEach(function () {
        module('main');

        // INJECT! This part is critical
        // $rootScope - injected to create a new $scope instance.
        // $controller - injected to create an instance of our controller.
        // $q - injected so we can create promises for our mocks.
        // _$timeout_ - injected to we can flush unresolved promises.
        inject(function ($rootScope, $controller, $q, _$timeout_,_$window_) {
            $scope = $rootScope.$new();
            $timeout = _$timeout_;
            $window = _$window_;
            $window.sessionStorage = {};
            $window.sessionStorage = storageMock();
            $window.sessionStorage.setItem('userName', 'Thao');
            $window.sessionStorage.setItem('userID', 10);
            $window.sessionStorage.setItem('profileID', 25);

            applicationCtrl = $controller('applicationController', {
                $scope: $scope,
                $window: $window
            });
        });
    });

    it('should return profile', function () {
        //$scope.currentUserID = 10;
        //$scope.currentUserName = 'Thao';
        //$scope.currentJobID = 62;
        //$scope.profile = null;

        $scope.GetProfile();
        expect($scope.profile).not.toBe(null);
    });
});

ฉันพยายามโดยไม่เรียก storageMock() แต่ก็ยังใช้งานไม่ได้ ขอบคุณ


person MsBugKiller    schedule 01.07.2016    source แหล่งที่มา
comment
ฉันสงสัยว่าโค้ดที่คุณกำลังทดสอบเป็นเพียงการเรียก sessionStorage แทนที่จะเป็น window.sessionStorage คุณคิดว่านั่นอาจเป็นปัญหาหรือไม่? หากเป็นเช่นนั้น การเยาะเย้ยของคุณควรตั้งค่าเป็น $rootScope.sessionStorage แทนที่จะเป็น $window.sessionStorage   -  person Christopher Davies    schedule 01.07.2016