You can use the AngularJS $provide . This page demonstrates how to mock a serviceβs functionality using Jasmine spies . Basically in your Jasmine test you can include:
var getDataMock; beforeEach(function() { getDataMock = { setData: jasmine.createSpy(), abort: jasmine.createSpy() }; module(function($provide) { $provide.value('getData', getDataMock); }); });
This tells AngularJS that instead of using the real getData service getData layout will be used in its place. In this example, using the Jasmine spy for the layout makes it easy to create expectations about how and when the getData service is called by the controller. You can alternatively install getDataMock on any other object, although it makes sense to maintain the same API as the real getData service.
For bonus points, you can specify the getDataMock object in a separate file, so the layout can be easily used in any number of unit tests where the test item uses getData . This, however, requires some configuration with any Jasmine runner that you use.
It is all assumed that you want a unit test controller. If you want the unit test getData service, AngularJS provides a nice mocking utility specifically for http requests.
source share