I am trying to test very simple AngularJS Service loading JSON data:
angular.module('archive', []) .factory('Loader', function ($http) { var result; $http.get('path').success(function(data) { result = data; console.log('returning: ', result); }); return { getData: result } });
Here is my test:
describe('TestArchive', function () { beforeEach(module('archive')); it('should load data', inject(function(Loader, $httpBackend){ $httpBackend .whenGET('path') .respond(22); var result = Loader.getData; $httpBackend.flush(); console.log(result); })); });
I was expecting to see 22
loaded but as I see from console
, it does not happen and result
is undefined
.
Any idea what is wrong?