AngularJS Jasmine Spies and Fakes

February 18, 2015

alt text

One of the great things about angular is it’s testability.

Say we have a directive that uses geolocation to display this information to the user if it is available and to display a message if it is not available.

So how can we test it using Jasmine and ensure that our test will work regardless if geolocation is available during our testing? We create a spy on the geolocation function and return a fake object instead.

Similarly we can return a fake error object to test that our directive acts correctly if geolocation is not available.