5

I have Angular (1.2.x) unit tests where I'm utilizing ngMock. My project has a fixture system that relies on sinon.fakeServer. For my unit tests, I'd prefer to use this as opposed to the $httpBackend .

ngMockE2E tests Angular provides a passthrough method, however there isn't a clear equivalent for unit tests. The rationale seems to be that unit tests should never be passing-through (to a server) but in my situation I'm just trying to pass through to a non-Angular-dependent-mock.

My strategy right now is to create a shim that matches .whenGet and .whenPost requests and routes them to my fake server.

However, what would be better is simply "turning off" the $httpBackend. Is there a way to do this?

vpiTriumph
  • 3,116
  • 2
  • 27
  • 39

2 Answers2

2

You may want to try

angular.module('httpReal', ['ng'])
    .config(['$provide', function($provide) {
        $provide.decorator('$httpBackend', function() {
            return angular.injector(['ng']).get('$httpBackend');
        });
    }])
    .service('httpReal', ['$rootScope', function($rootScope) {
        this.submit = function() {
            $rootScope.$digest();
        };
    }]);

This way you can restore httpBackend. For more details please refer: E2E mock $httpBackend doesn't actually passThrough for me

Community
  • 1
  • 1
Lalit Kale
  • 557
  • 6
  • 13
1

This solution might work.

So the proposition is to use ngMockE2E’s $httpBackend. Actually if your test cause XHR request, it is not a 'Unit Test'.

  • 3
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Enamul Hassan May 05 '16 at 00:15