我試圖測試我的$watch
偵聽器,但是它並未在測試中觸發。
這裏的測試:
it('should fire $watch when selectedBrands change', function() {
spyOn($scope, 'updateFilters')
$scope.selectedBrands = ['1'];
expect($scope.updateFilters).toHaveBeenCalled();
})
而且控制器:
$scope.selectedBrands = [];
$scope.$watch("selectedBrands", function() {
$scope.updateFilters();
}, true);
最後,錯誤:
Chrome 35.0.1916 (Mac OS X 10.10.0) ItemsController should fire $watch when selectedBrands change FAILED
Expected spy updateFilters to have been called.
Error: Expected spy updateFilters to have been called.
at null.<anonymous> (/Applications/MAMP/htdocs/cloqet_client/testing/tests/ItemModule/ItemsController_spec.js:62:38)
我嘗試使用$scope.$digest()
但使測試通過,即使沒有改變$scope.selectedBrands
。爲什麼$watch
不被解僱?
在下面的答案中發現了一些有趣的東西? – gkalpak