AngularJs Test,更改变量以覆盖if else块

玛丽安·阿卜杜勒·马里克(Marianne Abdelmalek)

我有一个控制器需要测试;覆盖范围对我来说至关重要。这是我来自控制器的完整代码:

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块?

彼得·阿什韦尔

您要测试的代码在实例化控制器时运行,这发生在您测试任何东西之前。您需要在it块中重新实例化控制器

it('does else', inject(function($rootScope) {
    $rootScope.serviceNumber = 'not m1';
    theController = $controller( ... instantiate again )
    expect(...
}));

更好的方法是对代码进行结构化,以使此逻辑流发生在服务中,或者至少发生在控制器中的函数中。然后,您可以测试它而无需制造另一个控制器

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章