在Angular JS karma grunt测试中管理依赖关系

时间:2015-03-12 05:44:23

标签: javascript angularjs unit-testing gruntjs karma-runner

我的控制器:

/**
 * @description
 * Controller for Comparative Analysis dashboard.
 */

(function (define) {

    define([], function () {

        /**
      * Constructor function.
      *
      * @param {Object} $scope Object that binds model to view.
      * @param {Object} $log object(angular service) to log info,waring and error messages.     
      *

      */
        //var caDashboardController = function ($scope, $log, configService, caCommonService, caConsumptionChartService) {
        var caDashboardController = function ($scope, $log) {




   }
        return ["$scope", "$log", caDashboardController];

    });


})(define);

我的模块:

(function (define, angular) {

    'user strict'

    define([

        'myAnalytics/comparativeAnalysis/dashboard/controllers/caDashboardController',

        ], function (caDashboardController) {


        var moduleName = "AnalyticsApp.comparativeAnalysis";

        angular.module(moduleName, ['ngRoute', 'ngResource', 'kendo.directives'])
        .config(['$routeProvider', function config($routeProvider) {

            $routeProvider.when('/comparativeAnalysis', {
                controller: 'caDashboardController',
                templateUrl: 'app/myAnalytics/comparativeAnalysis/dashboard/comparativeAnalysisdashboard.html'
            });

        }])
            .controller("caDashboardController", caDashboardController);


        return moduleName;
    });


})(define, angular);

我的单元测试用例规范:

(function (define) {
    'use strict';

    define([
        'kendo',
        'angularRoute',
        'angularResource',
        'moment',
        'myAnalytics/comparativeAnalysis/dashboard/comparativeAnalysisModule'
    ],
    function () {

        describe('Test for comparative Analysis Dashboard Controller', function () {



              var scopeMock, logMock, ctrl, configServiceMock, caCommonServiceMock, caConsumptionChartServiceMock;

                var serviceResponse = null;

                var deferredCompareResult;

                beforeEach(module('AnalyticsApp.comparativeAnalysis'));

                beforeEach(inject(function ($rootScope, $log, caCommonService, caConsumptionChartService) {
                    scopeMock = $rootScope.$new();
                    logMock = $log;

                    //configServiceMock = { comparativeAnalysisUrl: '/COMPARATIVEANALYSISURL/' };
                    configServiceMock = { csrServiceUrl: '/CSRSERVICEURL/' };//$injector.get('configService');
                    caCommonServiceMock = caCommonService;
                    caConsumptionChartServiceMock = caConsumptionChartService;
                }));

                describe('Successful Server response tests', function () {

                    beforeEach(inject(function ($controller, $q) {

                        deferredCompareResult = $q.defer();
                        deferredCompareResult.resolve(serviceResponse);

                        //spyOn(caCommonServiceMock, 'getData').and.returnValue(deferredCompareResult.promise);
                        //spyOn(caConsumptionChartServiceMock, 'getChartData').and.returnValue(deferredCompareResult.promise);

                        ctrl = $controller('caDashboardController',
                        {
                            '$scope': scopeMock,
                            '$log': logMock
                            //'configService': configServiceMock
                            //'caCommonService' :caCommonServiceMock,
                            //'caConsumptionChartService': caConsumptionChartServiceMock
                        });

                    }));

                    it('$scope should be configured', function () {
                        expect(ctrl).toBeDefined();
                    });

                    it('Consumption Graph Data should be set', inject(function ($rootScope) {
                        //expect(ctrl).toBeDefined();
                        //expect(scopeMock.caViewModel.chartData).toBeDefined();
                        ////scopeMock.caViewModel.search('');
                        //expect(caConsumptionChartServiceMock.getChartData).toHaveBeenCalled();
                        //$rootScope.$apply();
                        //expect(scopeMock.caViewModel.chartData).toBe(null);
                    }));

                });
            });
        });
    })(define);

在Ctrl中我只是引用'$scope''$log' -

ctrl = $controller('caDashboardController',
                    {
                        '$scope': scopeMock,
                        '$log': logMock
                        //'configService': configServiceMock
                        //'caCommonService' :caCommonServiceMock,
                        //'caConsumptionChartService': caConsumptionChartServiceMock
                    });

我收到的错误很少;

Error: [$injector:unpr] Unknown provider: configServiceProvider <- configService <- caCommonService

Error: Declaration Location

Error: Expected undefined to be defined.

请帮帮我如何配置。如你所见,我在控制器中没有使用任何东西。它几乎是空的,但我仍然得到这个错误.. 我没有configService的参考,但错误与此有关。

1 个答案:

答案 0 :(得分:1)

(function (define) {
    'use strict';

    define([
        'kendo',
        'angularRoute',
        'angularResource',
        'myAnalytics/comparativeAnalysis/dashboard/services/caCommomService',        'myAnalytics/comparativeAnalysis/dashboard/services/caConsumptionChartServic',       'myAnalytics/comparativeAnalysis/dashboard/controllers/caDashboardController', ],
    function (moment,caCommomService,caConsumptionChartServic,caDashboardController) {
var abcController;
 beforeEach(inject(function ($rootScope,$controller, $q, $timeout) {
        scope = $rootScope.$new();  
       q = $q;
       timeout = $timeout;
       abcController = $controller(caDashboardController, {
           $scope: scope,
           abcService: caConsumptionChartServic         
       });
   }));
// you are using requirejs dependencies so try to pass direct files and use no to get main module.
}