Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angular-mock

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?

like image 706
parliament Avatar asked Aug 03 '15 18:08

parliament


1 Answers

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.

like image 194
Lydon Avatar answered Dec 04 '22 08:12

Lydon