3

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?

1 Answer 1

1

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();
}));
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't seem to work anymore it still throws errors for some routes like /tapi/mds/info. Is there a better way?
Fundamentally this is a regex, so as a result you can probably just go with (/.*/).passThrough(), which is way more open ended but will let everything else through.
whats the point of that? does the order matter if I register new ones after that call?
nope, it does not just tested.... so this doesnt really answer the question then. I want to passthrough everything except what I explicity register.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.