Extraction of global HTTP requests for all sets in Angular and Jasmine

In my app.config I use a service called ui-router-extras FutureStates to dynamically create states from the results of a REST call. One of the side effects of this is that when my tests run, since I load my main application module in all of them , all tests call this service, and therefore all tests fail with

 Error: Unexpected request: GET /api/v1/config No more request expected 

I can add the following to my test packages, and it fixes the problem by registering this call with a backend so that it expects it.

 beforeEach(inject(function(_$httpBackend_){ _$httpBackend_.whenGET(/\/api\/v1\/config.*/).respond([]); })); 

The problem is that this will need to be added to each test suite, which in my modular application is a lot of wet code. What I'm looking for is either a way to determine this expectation globally once for all tests (either in the global karma file, or I even agree to import services / one line for placement in my sets), or if my test setup or app.config inefficiently configured to improve this.

What have i tried so far

 // Defined in a tests-global.js file listed in my karma files array beforeEach(inject(function(_$httpBackend_){ _$httpBackend_.whenGET(/\/api\/v1\/req_params.*/).respond([]); })); 

which fails with Error: Injector already created, can not register a module! since in my individual test beforeEach(module('x')); I call beforeEach(module('x')); to use module modules, and you cannot do this after inject() .

I also tried

 // Defined in a tests-global.js file listed in my karma files array beforeEach(function() { var $injector = angular.injector(['ngMock']); $httpBackend = $injector.get('$httpBackend'); $httpBackend.whenGET(/\/api\/v1\/req_params.*/).respond([]); }); 

which does not produce an additional error, but also does not solve the Unexpected request problem.

Is there a way to cope with expectations for all tests, or if it is a sign that my testing structure is not configured well, is there a better way to structure my code / tests?

+5
source share
1 answer

As for me, I do not include the main service module, but the layout for it is something like (since for me I include it inside karma.conf in the β€œfiles”):

 angular.module('some.service', []).service('MainService', function() { this.APP_HOST = 'localhost'; this.APP_PORT = 8000; }); 

Then inside the test in the global beforeEach (remember that MainService is mocked, not real):

 beforeEach(inject(function(_$httpBackend_, MainService, $rootScope, $controller) { $httpBackend = _$httpBackend_; MainService.APP_HOST = 'localhost'; MainService.APP_PORT = '8000'; baseUrl = '//' + MainService.APP_HOST + ':' + MainService.APP_PORT; scope = $rootScope.$new(); someCtrl = $controller('someCtrl', { $scope: scope, MainService: MainService }); })); 

And it will determine HOW if it will mock:

 function mockHttpSuccessfulRequest() { $httpBackend .expectGET(baseUrl + '/api/surveys') .respond({ surveys: [{ something: false, date: '2015-03-11T08:28:58.765Z' }] }); } 

So, you define this layout before testing:

beforeEach(mockHttpSuccessfulRequest);

And you check everything you want for free.

+1
source

All Articles