错误:[$ injector:unpr]未知提供者:initData

时间:2016-01-15 15:05:08

标签: angularjs jasmine karma-runner ngmock

我正在为angularjs应用程序编写单元测试。我使用业力,茉莉作为编写和测试单元测试用例的工具。

以下是代码组件:

app.js

 (function() {
      "use strict";
      angular.module("indirectTax.core", []);
      angular.module('indirectTax', ['mgcrea.ngStrap', 'ngRoute', 'ngSanitize', 'cfp.hotkeys', 'ngUpload', 'LocalStorageModule', 'indirectTax.core', 'angulartics', 'ng.shims.placeholder', 'brantwills.paging', 'ngMask']).config(['$routeProvider',
        function($routeProvider) {
          $routeProvider.when('/dashboard', {
            templateUrl: 'dashboard/dashboard.html',
            controller: 'DashboardController as ctrl'
          }).when('/terms/:id', {
            templateUrl: 'terms/terms.html',
            controller: 'TermsController as ctrl'
          }).otherwise({
            redirectTo: '/dashboard'
          });
        }
      ]).config(["$httpProvider",
        function($httpProvider) {
          // Disable IE9 Ajax Request Caching.
          if (!$httpProvider.defaults.headers.get) {
            $httpProvider.defaults.headers.get = {};
          }

        }
      ]).config(['hotkeysProvider',
        function(hotkeysProvider) {
          // Override the original hotkeys template, in order to add a wrapper
          hotkeysProvider.template = "<div ng-include=\"'hotkeys-template.html'\"></div>";
          hotkeysProvider.cheatSheetDescription = 'Show / Hide current shortcuts';
        }
      ]).run(['initData', 'core.lib.common', '$rootScope', 'hotkeys', 'localStorageService', '$location',
        function(initData, common, $rootScope, hotkeys, localStorageService, $location) {
          common.setConfig(initData.configFile);
          common.currentUser = initData.currentUser;
          $rootScope.$on('$viewContentLoaded', function(event) {
            angular.element('#main-footer').fadeIn(500);
          });
        }
      ]).constant("toastr", toastr).constant("interact", interact).constant("html2canvas", html2canvas);
    })();

用户明细-controller.js

var UsersDetailsController = (function () {
        function UsersDetailsController($controller, userService, $location, $route, common) {
            this.userService = userService;
            this.$location = $location;
            this.$route = $route;
            this.common = common;
            this.loading = true;
            this.predicate = "userName";
            this.reverse = false;
            this.searchKey = "";
            this.users = [];
            this.activeAmount = 0;
            this.inactiveAmount = 0;
            this.checkPermissions();
            this.init();
        }
        // Only root users should have access to the "User Details" page
        UsersDetailsController.prototype.checkPermissions = function () {
            if (this.common.currentUser.role !== this.common.constants.ROOT_ROLE) {
                this.$location.path("/dashboard");
            }
        };
        UsersDetailsController.prototype.init = function () {
            this.getUserList();
        };
        ///////////
        //API Calls
        //Get selected question set info
        UsersDetailsController.prototype.getUserList = function () {
            var self = this;
            self.loading = true;
            self.userService.getUsersList().then(function (data) {
                self.loading = false;
                self.users = data.users;
                self.activeAmount = data.activeUsersCount;
                self.inactiveAmount = data.inactiveUsersCount;
            }, function (data) {
                self.loading = false;
            });
        };

        UsersDetailsController.$inject = ['$controller', 'core.api.user', '$location', '$route', 'core.lib.common'];
        return UsersDetailsController;
    })();
    angular.module('indirectTax').controller('UsersDetailsController', UsersDetailsController);

用户明细控制器-spec.js

describe("user-details-controller", function () {

    // Before each test runs we're going to need ourselves an Angular App to test - go fetch!
    beforeEach(function () {
        module("indirectTax");  // module is an alias for angular.mock.module
    });

    // Tests for the sageDetail controller
    describe("usersDetailsController ->", function () {

        var $rootScope, $scope, $q, $location, common, userService, //controller depedencies
            usersDetailsController; // controller

        // Before each test runs set up the controller using inject - an alias for angular.mock.inject
        beforeEach(inject(function (_$controller_, _$rootScope_, _$q_, _$location_, _common_, _userService_) {
            $scope = {};
            // Note how each parameter is prefixed and suffixed with "_" - this an Angular nicety
            // which allows you to have variables in your tests with the original reference name.
            // So here we assign the injected parameters to the describe-scoped variables:
            $rootScope = _$rootScope_;
            $q = _$q_;
            $location = _$location_;
            common = _common_;
            userService = _userService_;

            // create a usersDetails controller and inject the dependencies we have set up
            usersDetailsController = _$controller_("UsersDetailsController", {
                $scope: $scope,
                $location: $location,
                common: common,
                userService: userService
            });

        }));

        it("controller should have initial predicate value 'userName'", function () {

            // tests this code has executed:
            //this.predicate = "userName";
            expect(usersDetailsController.predicate).toBe("userName");
        });
    });
});

SpecRunner.html

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Jasmine Spec Runner v2.3.4</title>

  <link rel="shortcut icon" type="image/png" href="libs/jasmine-2.3.4/jasmine_favicon.png">
  <link rel="stylesheet" href="libs/jasmine-2.3.4/jasmine.css">

  <script src="libs/jasmine-2.3.4/jasmine.js"></script>
  <script src="libs/jasmine-2.3.4/jasmine-html.js"></script>
  <script src="libs/jasmine-2.3.4/boot.js"></script>

  <!-- inject:vendor:js -->
  <script src="libs/jquery.js"></script>
  <script src="libs/angular.js"></script>
  <script src="libs/angular-mocks.js"></script>
  <script src="libs/angular-local-storage.js"></script>
  <script src="libs/angular-route.js"></script>
  <script src="libs/angular-sanitize.js"></script>
  <script src="libs/angular-shims-placeholder.js"></script>
  <script src="libs/angular-strap.js"></script>
  <script src="libs/angular-strap.tpl.js"></script>
  <script src="libs/angulartics.js"></script>
  <script src="libs/deferred-bootstrap.js"></script>
  <script src="libs/hotkeys.js"></script>
  <script src="libs/html2canvas.js"></script>
  <script src="libs/interact.js"></script>
  <script src="libs/moment.js"></script>
  <script src="libs/ng-upload.js"></script>
  <script src="libs/ngMask.min.js"></script>
  <script src="libs/paging.js"></script>
  <script src="libs/toastr.js"></script>
  <!-- endinject -->
  <!-- inject:js -->
  <script src="core/app.js"></script>
  <script src="core/lib/init.js"></script>
  <script src="core/api/user-service.js"></script>
  <script src="core/lib/analytics.js"></script>
  <script src="core/lib/common.js"></script>
  <script src="core/lib/ng-interact.js"></script>

  <!-- include source files here... -->
  <script src="admin/users-details-controller.js"></script>

  <!-- include spec files here... -->
  <script src="spec/admin/users-details-controller-spec.js"></script>

</head>

<body>
</body>

</html>

现在我正在触发http服务器并尝试运行SpecRunner.html文件。但是我得到了下面提到的错误:

enter image description here

init.js

var Core;
(function (Core) {
    var Lib;
    (function (Lib) {
        var Init = (function () {
            function Init($http, $q, common, user) {
                this.$http = $http;
                this.$q = $q;
                this.common = common;
                this.user = user;
                this.initData = new Core.Models.InitData();
            }
            Init.prototype.resolve = function () {
                var self = this;
                var deferred = this.$q.defer();
                this.$q.all([this.loadConfig()]).then(function () {
                    return self.getCurrentUser(self.getAPIURL());
                }).then(function () {
                    deferred.resolve(self.initData);
                });
                return deferred.promise;
            };
            Init.prototype.loadConfig = function () {
                var self = this;
                var deferred = this.$q.defer();
                self.$http.get(self.common.constants.CONFIG_FILE).then(function (response) {
                    self.initData.configFile = response.data;
                    deferred.resolve();
                });
                return deferred.promise;
            };
            Init.prototype.getCurrentUser = function (api_url) {
                var self = this;
                var deferred = this.$q.defer();
                self.user.getCurrent(api_url).then(function (user) {
                    self.initData.currentUser = user;
                    deferred.resolve();
                });
                return deferred.promise;
            };
            Init.prototype.getAPIURL = function () {
                if (this.initData.configFile) {
                    return this.initData.configFile.API_URL;
                }
                return this.common.constants.API_URL;
            };
            Init.$inject = ["$http", "$q", "core.lib.common", "core.api.user"];
            return Init;
        })();
        Lib.Init = Init;
        angular.module("indirectTax.core").service("core.lib.init", Init);
        angular.element(document).ready(function () {
            deferredBootstrapper.bootstrap({
                element: document.body,
                module: "indirectTax",
                injectorModules: "indirectTax.core",
                resolve: {
                    initData: ["core.lib.init", function (init) {
                        return init.resolve();
                    }]
                }
            });
        });
    })(Lib = Core.Lib || (Core.Lib = {}));
})(Core || (Core = {}));

有人可以指导我解决错误吗?

0 个答案:

没有答案