CAT CAT - 1 year ago 90
AngularJS Question

AngularJS unit test check if function without methods have been called

I'm using Jasmine to unit test my Angular app. It's pretty easy to test if a method of a function has been called using something like:

spyOn($rootScope, "$emit");

But I can't find a way to check when a function has been called (without a method), for e.g. I'm using
; in one controller and I have no idea where to apply the above code to this guy. I've seen some Jasmine examples where they were using
, but this doesn't work with Angular's DI.

Answer Source

I can't try it myself at the minute but maybe you could just inject a mock $anchorScroll instead?

var $anchorScroll = jasmine.createSpy('anchorScroll');

$controller('MyCtrl', {
    $anchorScroll: $anchorScroll


This should just create a blank spy, one which will take any arguments and do nothing but keep track of the calls for test usage.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download