在茉莉花测试中调用$ digest只是第一次使用

时间:2014-08-25 17:20:24

标签: angularjs jasmine

我正在尝试在我的示波器上测试“手表”的行为。但问题是它只被召唤一次。检查下面的小提琴。奇怪的是,我注意到如果你把变量放在控制器的$ scope而不是控制器本身(也就是$ scope.name vs vm.name)它实际上是有效的。

使用虚拟机(不工作):http://jsfiddle.net/2Ny8x/60/

//--- CODE --------------------------
(function (angular) {
    // Create module
    var myApp = angular.module('myApp', []);

    // Controller which counts changes to its "name" member
    myApp.controller('MyCtrl', ['$scope', function ($scope) {
        var vm = this;
        vm.name = 'Superhero';
        vm.counter = 0;
        $scope.$watch('vm.name', function (newValue, oldValue) {
            vm.counter = vm.counter + 1;
        });
    }]);      
})(angular);



// ---SPECS-------------------------

describe('myApp', function () {
    var scope,
    controller;
    beforeEach(function () {
        module('myApp');
    });

    describe('MyCtrl', function () {
        beforeEach(inject(function ($rootScope, $controller) {
            scope = $rootScope.$new();
            controller = $controller('MyCtrl', {
                '$scope': scope
            });
        }));
        it('sets the name', function () {
            expect(controller.name).toBe('Superhero');
        });

        it('watches the name and updates the counter', function () {
            expect(controller.counter).toBe(0);
            controller.name = 'Batman';
            scope.$digest();
            expect(controller.counter).toBe(1);
            controller.name = 'Superman';            
            scope.$digest();
            expect(controller.counter).toBe(2);
        });
    });



});

使用$ scope(有效):http://jsfiddle.net/2Ny8x/61/

//--- CODE --------------------------
(function (angular) {
    // Create module
    var myApp = angular.module('myApp', []);

    // Controller which counts changes to its "name" member
    myApp.controller('MyCtrl', ['$scope', function ($scope) {       
        $scope.name = 'Superhero';
        $scope.counter = 0;
        $scope.$watch('name', function (newValue, oldValue) {
            $scope.counter = $scope.counter + 1;
        });
    }]);      
})(angular);



// ---SPECS-------------------------

describe('myApp', function () {
    var scope,
    controller;
    beforeEach(function () {
        module('myApp');
    });

    describe('MyCtrl', function () {
        beforeEach(inject(function ($rootScope, $controller) {
            scope = $rootScope.$new();
            controller = $controller('MyCtrl', {
                '$scope': scope
            });
        }));
        it('sets the name', function () {
            expect(scope.name).toBe('Superhero');
        });

        it('watches the name and updates the counter', function () {
            expect(scope.counter).toBe(0);
            scope.name = 'Batman';
            scope.$digest();
            expect(scope.counter).toBe(1);
            scope.name = 'Superman';            
            scope.$digest();
            expect(scope.counter).toBe(2);
        });
    });



});

1 个答案:

答案 0 :(得分:1)

您正在观看范围内的属性,但事实并非如此,因为您正在修改控制器中的属性。因此,请将手表更改为: -

   $scope.$watch(function(){
       return vm.name;
    }, function (newValue, oldValue) {
        vm.counter = vm.counter + 1;
    });

<强> Plnkr

使其工作的另一种方法是在实例化控制器时在测试中使用controller as vm,以便控制器实例将作为名为vm的属性附加到作用域。

        controller = $controller('MyCtrl as vm', {
            '$scope': scope
        });

<强> Plnkr2