1
當我嘗試spyOn一個$範圍。$手錶的監聽功能,就像是從來不叫spyOnspyOn監聽監控功能不能正常工作
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
檢查js小提琴時,監視器的監聽器功能在控制器實例化時不運行。 –