samedi 27 février 2016

Why does the scope value not get set when testing?

I have created a unit test for my directive with angular 1.4.4:

angular.module('myApp', [])
    .directive('uiList', [
        function() {
            return {
                scope: {
                    lengthModel: '=uiList'
                },
                link: function(scope, elm, attrs) {


                    scope.$watch('lengthModel', function(newVal) {
                        scope.test=2;
                        console.log('kut');

                    });
                }
            };
        }
    ]);

this is my test:

describe('Testing $emit in directive', function() {
    var scope;
    var element;
    var elementScope;

    beforeEach(module('myApp'));

    beforeEach(inject(function($rootScope, $compile) {
        scope = $rootScope.$new();
        scope.row = 1;
        element = angular.element('<ul id="rows" ui-list="row">');
        $compile(element)(scope);
        scope.$digest();
        elementScope = element.scope();
    }));

    it('should change value', function() {

        scope.$apply(function() {
            scope.row = 1;
        });

        expect(elementScope.test).toEqual(2);


    });

});

When I run the test in webstorm I get this error:

Chrome 48.0.2564 (Mac OS X 10.10.5) Testing $emit in directive should emit FAILED              
        Expected undefined to equal 2.

I reaches the watch part of the directive because when I run karma I can see the log(=kut). How can I get a passing test?

Aucun commentaire:

Enregistrer un commentaire