我有一个我需要测试的控制器;报道对我来说至关重要...... 这是我来自控制器的完整代码:
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');
};
}
]);
测试看起来像:
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 else block', function() {
rootScope.serviceNumber = 'm2';
assert.equal(rootScope.serviceData, '{"phoneNumber" : "01005135309"}');
})
});
我的问题是我在哪里可以重新分配rootScope.serviceNumber变量以测试else块?
答案 0 :(得分:0)
您尝试测试的代码是在实例化控制器时运行的,这在您测试任何内容之前发生。您需要在it块中重新实例化控制器
it('does else', inject(function($rootScope) {
$rootScope.serviceNumber = 'not m1';
theController = $controller( ... instantiate again )
expect(...
}));
最好是构造代码,以便这个逻辑流发生在服务中,或者至少发生在控制器中的函数中。然后你可以在不制作另一个控制器的情况下测试它