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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With