I am trying to test a controller in my Angular framework:
.controller('UserCtrl', ['$scope', '$location', 'User', 'Config', function ($scope, $location, User, Config) { ... }])
This controller depends on several services that require the $ http object to invoke the server:
.factory('User', ['$http', function ($http) { var data = {}; return { query: function(oper, putdata, callback){ if(oper == 'get'){ $http.get(getUrl("user",null)).success(function(data2) { console.log(data2); callback(data2); data.userinfo = data2; }); }, userinfo: data }; }])
But when I try to load the controller, I cannot get httpBackend to work:
describe('UserCtrl', function(){ var ctrlScope, ctrl, $httpBackend, controllerService; beforeEach( inject(function($httpBackend, $http, $rootScope, $controller, User, Config) { _User = User; _Config = Config; spyOn(User, 'getUserInfo').andCallThrough(); //spyOn(User, 'query').andCallThrough(); ctrlScope = $rootScope.$new(); controllerService = $controller; httpMock = $httpBackend; }) ); it('should create setup userinfo object ', function() { httpMock.expectGET("/user/default/details"). respond({somejson}); ctrl = controllerService('UserCtrl', {$scope: ctrlScope, $location: location, User: _User, Config: _Config}); expect(_User.getUserInfo).toHaveBeenCalled(); httpMock.flush(); expect(ctrlScope.userinfo.length).toBe(1); }); });
All I ever get is:
Error: No pending request to flush !
is it possible to use httpBackend with the service that you called from the controller that you are testing?