8

Is it possible to use wild cards in angular mocks? for example:

$httpBackend.when('GET', '/api/checklists/*').respond({ userId: 'userX' }, { 'A-Token': 'xxx' });

rather than:

$httpBackend.when('GET', '/api/checklists/123').respond({ userId: 'userX' }, { 'A-Token': 'xxx' });
1
  • You meaning something like '/api/checklists/:id'
    – tymeJV
    Sep 4, 2013 at 15:57

1 Answer 1

13

The documentation suggests that it takes in a regular expression, so you can do something like this:

$httpBackend.when('GET', /\/api\/checklists\/[1-9][0-9]*/)

This will require the match an integer ID with a length of at least 1, not starting with zero. Of course, this is just an example. Create your own regular expressions to match.

2
  • 1
    Took me a while to get my head round the javascript regular expression syntax. Thanks
    – Al Polden
    Sep 16, 2013 at 7:58
  • Note that while regexes are supported, one $httpBackend expectation will still match only one request, and moreover, order or expectations is important. So if you have N requests happening, you have to still do (exactly) N $httpBackend.when calls. This limits the usefulness of the wildcards a lot.
    – jakub.g
    Apr 1, 2016 at 15:25

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.