在 Angular JS karma grunt 测试中管理依赖关系
Managing dependencies in Angular JS karma grunt testing
我的控制器:
/**
* @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
但错误与此有关。
(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.
}
我的控制器:
/**
* @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
但错误与此有关。
(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.
}