当我尝试监视 $scope.$watch 的侦听器函数时,就像从不调用 spyOn
http://jsfiddle.net/b8LoLwLb/1/
我的控制器
angular.module('angularApp')
.controller('MainCtrl', function ($scope) {
$scope.name = '';
this.changeName = function () {
console.log('the name has change to ' + $scope.name);
};
$scope.$watch('name', this.changeName);
});
我的测试
describe('Controller: MainCtrl', function () {
// load the controller's module
beforeEach(module('angularApp'));
var MainCtrl,
scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope: scope
});
}));
it('should check if watcher was triggered', function () {
// Spy the listener funtion
spyOn(MainCtrl, 'changeName');
// Change the watched property
scope.name = 'facu';
// Digest to trigger the watcher.
scope.$digest();
// Expect the function to have been called
expect(MainCtrl.changeName).toHaveBeenCalled();
});
});
问题是,测试执行它并打印控制台日志,而不是监视函数。
我正在使用角度 1.4