我有一个看起来像这样的控制器:
terminalController.controller('CashAcceptorController', [
'PaymentService',
'$rootScope',
'$scope',
'PayingInfo',
'$interval',
'$location',
function (PaymentService, $rootScope, $scope, PayingInfo, $interval, $location) {
$rootScope.currentPage = 'CASH_ACCEPTOR';
var number = '';
if ($rootScope.serviceNumber == 'm1') {
number = '011' + $rootScope.phoneNumber;
} else if ($rootScope.serviceNumber == 'm2') {
number = '010' + $rootScope.phoneNumber;
} else if ($rootScope.serviceNumber == 'm3') {
number = '012' + $rootScope.phoneNumber;
}
$rootScope.serviceData = '{"phoneNumber" : "' + number + '"}';
PaymentService.start(number);
$rootScope.cancelTimers = function () {
$interval.cancel(minutesTimer);
$interval.cancel(secondsTimer);
$interval.cancel(getPayingInfo);
getPayingInfo = undefined;
minutesTimer = undefined;
secondsTimer = undefined;
$location.path('/');
};
$scope.seconds = 59;
$scope.minutes = 4;
var minutesTimer = $interval(function () {
if ($scope.minutes > 0) {
$scope.minutes--;
}
}, 60000);
var secondsTimer = $interval(function () {
if ($scope.minutes == 0 && $scope.seconds == 0) {
PaymentService.timeout();
} else if ($scope.seconds == 0 && $scope.minutes != 0) {
$scope.seconds = 59;
} else if ($scope.seconds != 0) {
$scope.seconds--;
}
}, 1000);
//Call getPayingInfo() once every second
var getPayingInfo = $interval(function () {
PayingInfo.get() .$promise.then(function (response) {
$scope.PayingInfo = response;
$scope.acceptedAmount = $scope.PayingInfo.acceptedAmount;
$scope.amountToPay = $scope.PayingInfo.amountToPay;
$scope.commissionAmount = $scope.PayingInfo.commissionAmount;
$scope.currency = $scope.PayingInfo.currency;
$scope.state = $scope.PayingInfo.state;
$scope.commissionRules = $scope.PayingInfo.paymentPlan.commissionRules;
$scope.minAmount = $scope.PayingInfo.paymentPlan.commissionRules[0].fromAmount + $scope.PayingInfo.paymentPlan.commissionRules[0].minValue;
}, function (errResponse) {
//error
});
}, 1000);
$rootScope.cancelPayment = function () {
PaymentService.cancel();
$interval.cancel(getPayingInfo);
getPayingInfo = undefined;
$location.path('/');
};
$scope.$on('$routeChangeStart', function (next, current) {
$interval.cancel(minutesTimer);
$interval.cancel(secondsTimer);
$interval.cancel(getPayingInfo);
getPayingInfo = undefined;
minutesTimer = undefined;
secondsTimer = undefined;
});
$scope.printReceipt = function () {
$location.path('/CompletePayment');
};
}
]);
我想测试一下: 1-当我调用$ rootScope.cancelTimers()时,间隔全部被删除(这里的定时器是局部变量,在尝试断言时总是未定义)
我正在和Chai一起使用Karma,请不要使用Jasmine特定声明。
我的测试看起来像:
describe('CashAcceptorController', function() {
var PaymentService, rootScope, scope, PayingInfo, $interval, $location;
beforeEach(module('eshtaPayTerminalApp.controllers'));
beforeEach(module('eshtaPayTerminalApp.services'));
beforeEach(inject(function($controller,
$rootScope, _$window_, _PaymentService_, _$interval_, _PayingInfo_) {
$interval = _$interval_;
scope = $rootScope.$new();
rootScope = $rootScope.$new();
$window = _$window_;
PaymentService = _PaymentService_;
PayingInfo = _PayingInfo_;
rootScope.serviceNumber = 'm1';
rootScope.phoneNumber = '05135309';
$controller('CashAcceptorController', {
$rootScope : rootScope,
$scope : scope,
$location : $location,
_PaymentService_ : PaymentService,
_$interval_:$interval,
_PayingInfo_:PayingInfo
});
}));
it('should set initial page variables', function() {
assert.equal(rootScope.currentPage, 'CASH_ACCEPTOR');
assert.equal(scope.seconds , 59);
assert.equal(scope.minutes , 4);
assert.equal(rootScope.serviceData, '{"phoneNumber" : "01105135309"}');
});
it('should visit the first else-if-block', inject(function($controller,
$rootScope, _PaymentService_, _$interval_, _PayingInfo_) {
$interval = _$interval_;
scope = $rootScope.$new();
rootScope = $rootScope.$new();
PaymentService = _PaymentService_;
PayingInfo = _PayingInfo_;
rootScope.serviceNumber = 'm2';
rootScope.phoneNumber = '11111111111';
$controller('CashAcceptorController', {
$rootScope : rootScope,
$scope : scope,
$location : $location,
_PaymentService_ : PaymentService,
_$interval_:$interval,
_PayingInfo_:PayingInfo
});
assert.equal(rootScope.serviceData, '{"phoneNumber" : "01011111111111"}');
}));
it('should visit the second else-if-block', inject(function($controller,
$rootScope, _PaymentService_, _$interval_, _PayingInfo_) {
$interval = _$interval_;
scope = $rootScope.$new();
rootScope = $rootScope.$new();
PaymentService = _PaymentService_;
PayingInfo = _PayingInfo_;
rootScope.serviceNumber = 'm3';
rootScope.phoneNumber = '11111111111';
$controller('CashAcceptorController', {
$rootScope : rootScope,
$scope : scope,
$location : $location,
_PaymentService_ : PaymentService,
_$interval_:$interval,
_PayingInfo_:PayingInfo
});
assert.equal(rootScope.serviceData, '{"phoneNumber" : "01211111111111"}');
}));