gpt4 book ai didi

javascript - Jasmine /Angular : Unknown provider: $localStorageProvider <-localStorage

转载 作者:行者123 更新时间:2023-12-03 06:24:40 27 4
gpt4 key购买 nike

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

当我使用 Karma 运行 jasmine 测试时遇到错误。我不确定如何处理它,我尝试将本地存储放入我的 Jasmine 文件中的注入(inject)中,但没有运气。我想要做的就是确保我的 Controller 被我的 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:有趣的是,如果我注释掉 it block ,测试运行时不会出现错误。

最佳答案

你必须在 module('angularTemplateApp'); 之前注入(inject) ng 存储

beforeEach(module('ngStorage')); 一样,前提是您已将 localstorage 文件添加到配置文件中,即 ngStorage.min.js

关于javascript - Jasmine /Angular : Unknown provider: $localStorageProvider <-localStorage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38711444/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com