Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS : $httpBackend mock only some requests

I use ngMock module to mock a several requests like so:

 $httpBackend.whenGET("/accounts").respond([obj]);

However, it seems that loading the module expects you to mock ALL requests. So if I do any other request other than those I mocked, I get "Unexpected request" error.

How can I configure it so that it ONLY intercepts requests that I mock explicitly, and passes everything else through?

like image 645
parliament Avatar asked Feb 18 '26 11:02

parliament


1 Answers

You can use a regex and the passThrough() function built into $httpBackend.

beforeEach(inject(function($httpBackend){
    $httpBackend.whenGET("/accounts").respond([obj]);
    //Pass everything else through
    $httpBackend.whenGET(/^\w+.*/).passThrough();
}));
like image 76
mikeswright49 Avatar answered Feb 20 '26 23:02

mikeswright49



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!