Created
August 7, 2013 13:46
-
-
Save leandromoreira/6174185 to your computer and use it in GitHub Desktop.
Example of testing angular (angularjs) factory using http.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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