Mocking only specific HTTP requests

76 views
Skip to first unread message

Andrea Reginato

unread,
Apr 24, 2013, 10:14:20 AM4/24/13
to ang...@googlegroups.com
Hi everyone, 

I'm having a weird use case that is giving me hard time. Here the scenario. I've build a directive where I need to simulate some HTTP requests. To make this happen I've used angular-mock. Follows a simplified example.

// Directive definition
var app = angular.module('my.directive', [ 'lelylan', 'ngMockE2E' ]);

app.run(['$httpBackend', function($httpBackend) {
  // mock the desired request

  // enable all other requests
  $httpBackend.whenGET(/.*/).passThrough();
  $httpBackend.whenPOST(/.*/).passThrough();
  $httpBackend.whenPUT(/.*/).passThrough();
  $httpBackend.whenDELETE(/.*/).passThrough();
}]);

With the code above I'm able to intercept the request to `api.lelylan.com/devices/1` and to let all other requests happen easily. Now, the problems comes when I use this directive in another project, because If I want to add other mocks to HTTP requests they will not be considered.

// App definition
var app = angular.module('app', ['my.directive']);

app.run(['$httpBackend', function($httpBackend) {
  // mock the desired request
  $httpBackend.whenGET('http://example.com/buba').respond(json);
}]);

What happen is that the mock to `http://example.com/buba` is not working, because of the previous `passThrough()` set in the component. Do you have any idea on how can I solve this problem?

Thanks a lot.

Reply all
Reply to author
Forward
0 new messages