0

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");
expect($rootScope.$emit).toHaveBeenCalled();

But I can't find a way to check when a function has been called (without a method), for e.g. I'm using $anchorScroll(); 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 expect(window.myFunction()).toHaveBeenCalled(), but this doesn't work with Angular's DI.

1 Answer 1

7

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
});

expect($anchorScroll).toHaveBeenCalled();

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I had the impression I tried that also ... but it seems I was doing something wrong. It works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.