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?