angular-mocks - only mock explicit request. passthrough all unexpected requests
Asked Answered
G

1

16

I find it incredibly frustrating that angular-mocks blocks all requests BY DEFAULT, and forcing me to "passthrough" what I want.

Sometimes I simply want to test 1 url with a mock and I have to jump through serious hoops for every "Unexpected Request" error.

I don't know regex, I don't like regex, I dont want to use regex!

Looks at this hideous code I need for ONE simple mock

 $httpBackend.whenGET(/\/atlas\/account\/[0-9]+$/)
     .respond(atlasAccounts[0]);

  $httpBackend.whenGET(/\/scripts$/).passThrough();     
  $httpBackend.whenGET(/^\w+.*/).passThrough();     
  $httpBackend.whenPOST(/^\w+.*/).passThrough(); 

Why can't this just be reduced to one line???

 $httpBackend.whenGET(/\/atlas\/account\/[0-9]+$/)
     .respond(atlasAccounts[0]); 

Or even better, why doesn't it support damn wildcards? Are they trying to make developers' lives harder?

  $httpBackend.whenGET("/atlas/account*") 
     .respond(atlasAccounts[0]);

That's all I need, if only it was this intuitive...

Is there any way to DISABLE this all-or-nothing convention in ngMock and ONLY intercept urls I EXPLICITLY mock?

Gutty answered 3/8, 2015 at 18:44 Comment(1)
Seriously i totally agree spent days on this, I had no idea it blocks all other request by default.Publicness
B
10

Add all your captures first then finally add a passthrough for all. I am doing something like this in my application and is working great:

function run($httpBackend) {
    var phones = [{ name: 'phone1' }, { name: 'phone2' }];

    // Capture specific request
    $httpBackend.whenGET('/phones').respond(function () {
        return phones;
    });

    // Passthrough everything
    $httpBackend.whenGET(/[\s\S]*/).passThrough();
}

This will capture '/phones'. If it doesn't capture, it will pass-through.

Brittaney answered 10/12, 2015 at 5:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.