Search code examples
angularjskarma-jasminekarma-mocha

AngularJS & mocha: $httpBackend call before each test


I am working with angularjs and writing tests in mochajs. I implemented multi-language support in my application and now I have the problem, that in many test-cases i get an unexpected request for my language.json

Do you have any idea how to handle this, without adding the "$httpBackend.whenGet(....." for the language-handling to each test?


Solution

  • Both Mocha and Jasmine support root-level hooks (which will affect all describes).

    When loaded in the first place in karma, this

    angular.module('test-setup', []).run(function ($httpBackend) {
      $httpBackend.whenGET(...
    });
    
    beforeEach(module('test-setup'));
    

    should provide $httpBackend setup for all specs.