Skip to content

Instantly share code, notes, and snippets.

@leandromoreira
Created August 7, 2013 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leandromoreira/6174185 to your computer and use it in GitHub Desktop.
Save leandromoreira/6174185 to your computer and use it in GitHub Desktop.
Example of testing angular (angularjs) factory using http.
angular.module('pearlJam')
.factory('Songs', function($http, $timeout, Config){
var response = {list: []};
var onSuccess = function(result){
response.list = result.data.data;
$timeout(poller, Config.pollingTimeout);
};
var poller = function(){
$http.get('api/songs.json', Config.httpOptions).then(onSuccess);
};
poller();
return {all: response};
});
describe('Songs', function(){
var httpStub, localService;
beforeEach(module('Sauron'));
beforeEach(inject(function(_$httpBackend_, Streams){
httpStub = _$httpBackend_;
localService = Streams;
}));
it('lists all songs', function(){
var httpResponse = { data: [1]};
httpStub.whenGET('api/songs.json').respond(httpResponse);
var serviceResponse = localService.all;
httpStub.flush();
expect(serviceResponse).toBe([1]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment