How would I test a $scope.watch (AngularJS) change in Jasmine?

我的梦境 提交于 2019-12-21 03:08:12

问题


I've just recently started writing something with AngularJS and I'm not sure how to go about writing a test for this particular thing. I'm building a "Help Request" mode that has different states. So in my controller, I use a $scope.request_mode variable. The different links to activate help requests set that variable to something differently.

Then inside my directive, I'm doing a $scope.$watch('request_mode', function(){...}); to selectively activate or deactivate things as the request mode changes. The code all works great, but the problem I'm having is with testing. I cannot seem to get Jasmine to pick up the $scope.$watch and actually fire anything when it changes.

I'm sure someone has run into this before, so any suggestions would be very much appreciated.


回答1:


In your unit tests you need to manually call $scope.$digest() or $scope.$apply() to trigger $scope.$watch().

Normally in your code you wouldn't have to do this, since directives like ng-click do $rootScope.$apply for you behind the scenes.



来源:https://stackoverflow.com/questions/17112446/how-would-i-test-a-scope-watch-angularjs-change-in-jasmine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!