Testing $scope.$watch in Jasmine?

2,236 views
Skip to first unread message

Michael Pelletier

unread,
Jun 14, 2013, 2:14:25 PM6/14/13
to ang...@googlegroups.com

Hey guys,

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" feature which has different states, for different kinds of help that can be requested. One of the modes allows you to select elements of the page to get help on. I'm using $scope.request_mode to track what type of Help is requested, where the different links to activate a help request set the variable to have a different string, triggering a different behaviour. 

Inside of the directive, I'm tracking the changes to the request mode by 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 the testing - I cannot seem to get Jasmine to pick up the $scope.$watch and actually fire anything when I trigger a change on it inside of the tests. 

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

Michael Bielski

unread,
Jun 14, 2013, 2:37:24 PM6/14/13
to ang...@googlegroups.com
To cause a $timeout to happen, inject $timeout into your it() and then call $timeout.flush() when you need it to happen.

Michael Pelletier

unread,
Jun 14, 2013, 3:00:37 PM6/14/13
to ang...@googlegroups.com
On Friday, June 14, 2013 2:37:24 PM UTC-4, Michael Bielski wrote:
To cause a $timeout to happen, inject $timeout into your it() and then call $timeout.flush() when you need it to happen.

Sorry, can you possibly elaborate for me? I'm not exactly sure how a $timeout would solve the problem.

Michael Bielski

unread,
Jun 14, 2013, 3:37:53 PM6/14/13
to ang...@googlegroups.com
Bah... managed to copy the wrong line of my notes! Sorry! The correct line of my notes says:

Calling scope.$digest() will cause a $watch to fire.
Reply all
Reply to author
Forward
0 new messages