mercredi 19 août 2015

AngularJS Unit Test - How to mock button pressed with attributes

I have this html with this controller:

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.
}

So, the user press the button with the set() function wich gets the index value and store it in stringConcat variable. This is working fine. Now I want to make an unit test, but how can I mock the this.i thing?

unit test

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');

    });
});

Aucun commentaire:

Enregistrer un commentaire