gpt4 book ai didi

javascript - 管理 Angular JS karma grunt 测试中的依赖关系

转载 作者:行者123 更新时间:2023-12-03 10:35:33 25 4
gpt4 key购买 nike

我的 Controller :

/**
* @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.

请帮助我如何配置它。正如你所看到的,我没有在 Controller 中使用任何东西。它几乎是空的,但我仍然收到这个错误..我没有 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.
}

关于javascript - 管理 Angular JS karma grunt 测试中的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29002664/

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