控制器代码
var dashboardApp = angular.module('do.dashboard', [])
dashboardApp.controller('developerSummaryController', ['$scope', '$filter', '$routeParams', 'developerSummaryService',
function ($scope, $filter, $routeParams, summaryBoardService) {
$scope.group = {};
var _getStatusText = function (statusValue) {
return statusValue <= 33 ? 'RED' : (statusValue <= 66 ? 'YELLOW' : (statusValue <= 100 ? 'GREEN' : 'GREEN'))
}
var _parseSummaryData = function (summaryValue) {
summaryValue.OverallStatusText = _getStatusText(summaryValue.OverallStatus);
summaryValue.BuildUTStatusText = _getStatusText(summaryValue.UnitTesting);
summaryValue.CodeQualityStatusText = _getStatusText(summaryValue.CodeQuality);
summaryValue.Deploy.DevStatusText = _getStatusText(summaryValue.Deploy.Dev);
summaryValue.Deploy.QAStatusText = _getStatusText(summaryValue.Deploy.QA);
summaryValue.Deploy.ProdStatusText = _getStatusText(summaryValue.Deploy.Production);
angular.forEach(summaryValue.Components, function (componentValue, componentKey) {
componentValue.OverallStatusText = _getStatusText(componentValue.OverallStatus);
componentValue.BuildUTStatusText = _getStatusText(componentValue.UnitTesting);
componentValue.CodeQualityStatusText = _getStatusText(componentValue.CodeQuality);
componentValue.Deploy.DevStatusText = _getStatusText(componentValue.Deploy.Dev);
componentValue.Deploy.QAStatusText = _getStatusText(componentValue.Deploy.QA);
componentValue.Deploy.ProdStatusText = _getStatusText(componentValue.Deploy.Production);
componentValue.BuildTime = $filter('date')(componentValue.BuildTime, 'yyyy-MM-dd HH:mm:ss a');
});
return summaryValue;
}
summaryBoardService.getSummaryBoardData($routeParams.groupName).then(
function success(response) {
$scope.group = _parseSummaryData(response.data);
}, function error(reason) {
console.log(reason);
});
} ]);
规格
describe('developerSummaryController', function () {
var scope, ctrl, mockSefService, $timeout;
beforeEach(module('do.dashboard'));
beforeEach(inject(function ($rootScope, $controller, $q, _$timeout_) {
mockSefService = jasmine.createSpyObj('developerSummaryService', ['getSummaryBoardData']);
scope = $rootScope.$new();
$timeout = _$timeout_;
mockSefService.getSummaryBoardData.and.returnValue($q.when(
{data: { Name: 'Client Management', OverallStatus: 15.777499999999998, CodeQuality: 10.5, UnitTesting: 28.5, CodeCoverage: 8.3325 }}
));
ctrl = $controller('developerSummaryController', {
$scope: scope,
developerSummaryService: mockSefService,
$routeParams: { id: 'Client Management' }
});
}));
it('controller should not be null', function () {
expect(ctrl).not.toBe(null);
});
it('should set the scope data with route parameter', function () {
expect(scope.groupName).toEqual('Client Management');
});
it('should call the service method and set the scope data', function () {
expect(mockSefService.getSummaryBoardData).toHaveBeenCalled();
$timeout.flush();
expect(scope.group.Name).toEqual('Client Management');
expect(scope.group.OverallStatus).toEqual(15.777499999999998);
expect(scope.group.CodeQuality).toEqual(10.5);
});
});
这里的控制器我已经写了测试,最后两个都失败了,任何人都可以通过解决方案或者我可能在这里做错了。 这里控制器文件有一个叫做服务的Json,我只为控制器创建单元测试。