首页 > 解决方案 > 模块“分析”不可用!您要么拼错了模块名称,要么忘记加载它

问题描述

我在控制器中添加了一个新的依赖项,并且由于现有的测试用例文件开始显示此错误。

Error: [$injector:modulerr] Failed to instantiate module Analytics due to:

[$injector:nomod] Module 'Analytics' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

这是 spec.js 文件

describe("Module filter Navigator Controllers Test",function(){
    var $controller, $rootScope, $httpBackend, filtersNavigatorCtrl, $scope, $q, $timeout, $compile;

    beforeEach(function(){      
        module('Analytics', function($provide) {
            $provide.service('Analytics', function($q) {
                this.trackEventFull = jasmine.createSpy('trackEventFull').andCallFake(function() {
                    var defer = $q.defer();
                    var promise = defer.promise;
                    promise.success = function(fn) {
                        promise.then(function(response) {
                          fn(messages, response.status, response.headers);
                        });
                      };
                    return promise;
                });
              });
        });

    beforeEach(inject(function(_$controller_,_$rootScope_,_$httpBackend_,_$q_, _$timeout_,_$compile_,_$filter_){
        $q = _$q_;
        $controller = _$controller_;
        $rootScope = _$rootScope_;
        $httpBackend = _$httpBackend_;
        $timeout = _$timeout_;
        $compile = _$compile_;
        $filter_ = _$filter_;
        $scope = $rootScope.$new();
        filtersSelectionCtrl = $controller('filtersNavigatorCtrl',{$scope:$scope});
    }));

这是 FilterNavigatiorCtrl.js

filtersControllers.controller('filtersNavigatorCtrl', ['$filter', '$scope', '$rootScope', '$timeout', '$compile', 'filterService', 'regionsService', 'Analytics',
    function ($filter, $scope, $rootScope, $timeout, $compile, filterService, regionsService, Analytics) {
   // controller code here 
    }

这是同一应用程序中的另一个控制器 filtersControllers.js。

var filtersControllers = angular.module('filterControllers', ['filterServices', 'regionsServices', 'messagesModule']);

这个模块的 app.js 看起来像这样

angular.module('filtersApp', [
        'filterControllers',
        'viewQueryControllers',
        'entitlementModule',
        'messagesModule',
        'ngSanitize',
        'ngResource'
    ]);

我想模拟AnalyticsFilterNavigatiorCtrl.js 文件使用的模块,但是当我这样做时,我得到了上述错误。如何解决这个问题?

标签: angularjsunit-testingjasmine

解决方案


推荐阅读