我在我的控制器的以下功能:使用因緣測試上rootscope的功能,而不是一個功能
.controller('ContainerController', ['$scope', '$rootScope', '$state','$window', '$location','employeeFactory', 'employeeTestFactory', function ($scope, $rootScope, $state, $window,$location, employeeFactory, employeeTestFactory) {
$rootScope.calculateUsed = function(val){
$rootScope.employee.timePending = $rootScope.employee.timePending = 0;
var newTimeUsed = 0;
angular.forEach(val, function(key, value){
var td = key.timeDuration;
if(key.timeState === "pending"){
$rootScope.employee.timePending += Number(td);
}else{
newTimeUsed += Number(td);
}
});
$rootScope.employee.totalTimeUsed = newTimeUsed;
}
}])
在我的人緣我想聲明和使用功能:
describe('Controllers', function() {
var InfoController, ContainerController, state;
var $controller, $location, $window;
var scopeIC, scopeCC;
beforeEach(module('ui.router'));
beforeEach(angular.mock.module('ptoApp.controller'));
beforeEach(angular.mock.module('ptoApp.employeeTestFactory'));
beforeEach(module('ngResource'));
beforeEach(inject(function($controller, _$rootScope_, _$location_, _$window_, _employeeTestFactory_, _$httpBackend_, _$state_) {
scopeIC = _$rootScope_.$new();
scopeCC = _$rootScope_.$new();
$rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
ContainerController = $controller('InfoController', {
$scope: scopeCC,
$location: _$location_,
$window: _$window_,
$rootScope: $rootScope,
employeeTestFactory:_employeeTestFactory_,
$state:_$state_
});
}));
it('ContainerController should be defined', function() {
expect(ContainerController).toBeDefined();
});
it('ContainerController should test calculateUsed on rootscope', function() {
$rootScope.employee = {
"employeeid": "[email protected]",
"firstName": "Todd",
"lastName": "Coulson",
"employeeType": "full-time",
"totalTimeUsed": 0,
"totalTimeAccrued": 40
}
var singleReq = [{"requestedBy": "Todd",
"approvedBy": "Tom",
"status": "approved",
"startDateTime": "3/25/2017",
"endDateTime": "3/29/2017",
"timeDuration": "32",
"message": "I need a vaca",
"approverMessage": "Take one!",
"locked": "false",
"timeState":"approved",
"timeType":"multiple days",
"timeOffGroup":"paid time off"}]
$rootScope.calculateUsed(singleReq);
expect($rootScope.employee.totalTimeUsed).toBe(32);
});
});
任何人都可以告訴我爲什麼calculateUsed不是這裏的函數嗎?我正在傳遞rootScope,我可以驗證它已經定義,但是它不能讀取我已經放在$ rootScope中的函數?提前致謝。
要調用的ContainerController爲「InfoController」是正確的? ContainerController = $控制器('InfoController',{...}) – RafaelTSCS
這是我的錯誤。謝謝你的收穫。 – tcoulson
如果你把它放在答案的形式,我會給你信用。 – tcoulson