AngularJS单元测试 - 如何模拟按下属性的按钮

时间:2015-08-20 00:12:39

标签: javascript angularjs unit-testing

我有这个控制器的HTML:

HTML

<div ng-controller="MyController">
        <span ng-repeat="(i, number) in numbers">
            <span id="btn_{{i}}" type="button" ng-click="set()">
                {{ number }}
            </span>
        </span>
</div>

myController的

var temp;

$scope.set = function() {
    temp += this.i; // Get the index of <span>
    $rootScope.stringConcat = temp; // Agrega la cadena a la caja de texto.
}

因此,用户按下具有set()功能的按钮,获取索引值并将其存储在stringConcat变量中。这工作正常。现在我想进行单元测试,但是如何模拟this.i

单元测试

describe('MyController', function() {

    var $rootScope, createController;

    beforeEach(module('myapp'));

    // 
    beforeEach(inject(function($injector) {
        $rootScope = $injector.get('$rootScope');
        var $controller = $injector.get('$controller');

        createController = function() {
            return $controller('MyController', {'$scope' : $rootScope});
        }
    }));


    it('Should store values', function() {

        var controller = createController();

        $rootScope.set('0'); // This is obvious not working.
        $rootScope.set('1');
        $rootScope.set('2');
        $rootScope.set('7');

        expect($rootScope.password).toEqual('0127');

    });
});

0 个答案:

没有答案