Jasmine / Angular:未知提供者:$ localStorageProvider< -localStorage

时间:2016-08-02 03:46:10

标签: javascript angularjs jasmine

Error: [$injector:unpr] Unknown provider: $localStorageProvider <- $localStorage

当我用Karma进行茉莉花测试时,我收到了一个错误。我不确定如何处理它,我尝试在我的jasmine文件中注入localstorage,但没有运气。我想做的就是确保我的控制器被我的Jasmine文件识别以启动。

Javascript文件:

define(['app'], function (app) {
    'use strict';

    app.controller('SplashController',
        function ($scope, $state, $localStorage, $window, $modal, pageService, authenticationService, focusService, config) {

            var state;
            focusService.focusMain();

            $scope.beginLogon = function () {
                authenticationService.authenticate();
            };

            $scope.checkMedia = function (state) {
                $state.transitionTo(state);
            };
            $scope.today = new Date();
            $scope.appTitle = config.app.title;

            var loc = $window.location.pathname;
            var url = $window.location.origin;


var modalInfo = {value: null, dismissed: true, resendHash: []};
  var modalInstance;
  var controller;
  var openModal = function (template, controller, modalInfo) {

    modalInstance = $modal.open({
      windowTemplateUrl: 'src/ui-components/modals/helper/modal-window_template.html',
      templateUrl: template,
      controller: controller,
      backdrop: 'static',
      keyboard: false,
      resolve: {
        modalInfo: function () {
          return modalInfo;
        }
      }
    });
  };


  $scope.previewCloseClick = function () {
    $modalInstance.close();

    modalInfo.dismissed = true;
  };

  var ModalInstanceCtrl = function ($scope, focusService) {
    modalInfo.dismissed = false;

    focusService.focusTopModal();

    $scope.okCancel = function () {
      modalInstance.close();
      modalInfo.dismissed = true;
    };
  };



        });
});

规格文件:

define(['SplashController'], function () {
    'use strict';

    describe("The Splash Controller", function () {
        var controller,
            scope,
            localStorage;

        beforeEach(function () {
            module('angularTemplateApp');
            focusServiceMock = jasmine.createSpyObj('focusService', ['focusMain']);


            module(function ($provide) {
                $provide.value('focusService', focusServiceMock);
                $provide.value('authenticationService', authServiceMock);
            });

            inject(function ($controller, $rootScope, $localStorage) {
                localStorageMock = jasmine.createSpyObj('$localStorage', ['openModal']);
                scope = $rootScope.$new();
                localStorage = $localStorage;
                controller = $controller('SplashController', {
                    $scope: scope, 
                    $localStorage: localStorage

                });

            });
        });

        it("when the login button is clicked", function () {

        expect(controller).toBeDefined();
        });
    });
});

更新1:有趣的是,如果我注释掉它的阻止,那么测试会毫无错误地运行。

1 个答案:

答案 0 :(得分:1)

您必须在module('angularTemplateApp');

之前注入存储空间

beforeEach(module('ngStorage'));类似,只要您已将localstorage文件添加到配置文件中,即ngStorage.min.js