Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mock $httpBackend regex not matched

Tags:

angularjs

I'm trying to match an url with a regex. But the follwing regex doesn't match.

$httpBackend.whenGET('/^rest\/find-reservations\?.*/)').respond(function () {
        return [200, ['succes'], {}];
    });

I keep getting the follwing error:

Error: Unexpected request: GET rest/find-reservations?end=1421424299193&reservationClass=Reservation&start=1358352299193
No more request expected

When I change the regex to an absolute string '/find-reservations' the whenGET is triggered. Why is this?

edit: I'm mocking a backend. http://plnkr.co/edit/VF4KbZO3FvngWQsiUcte?p=preview The following plnkr works fine for static urls and partials. But it does not in the above case.

like image 599
Vincent Avatar asked Jan 16 '14 17:01

Vincent


2 Answers

If you want to match this url:

"rest/find-reservations?end=1421424299193&reservationClass=Reservation&start=1358352299193"

use this code:

$httpBackend.whenGET(/^rest\/find-reservations\?.*/).respond(function () {
  return [200, ['success'], {}];
});

If you see this error Error: Unexpected request:

It can be for some reasons:

  • You forget $httpBackend.expectGET(url).
  • The order of expectGET should be the same as the order of the requests.
  • You called $httpBackend.verifyNoOutstandingExpectation() before $httpBackend.flush().

It is not related to $httpBackend.whenGET at all.

From the $httpBackend docs:

Request expectations provide a way to make assertions about requests made by the application and to define responses for those requests. The test will fail if the expected requests are not made or they are made in the wrong order

like image 86
Ilan Frumer Avatar answered Oct 02 '22 18:10

Ilan Frumer


There are two problems with your code:

  1. Enclosing an expression in '...' makes it a string (even if it looks like a regular expression.

  2. You must include the leading / in your pattern.


Your code should be modified like this:

$httpBackend.whenGET(/^\/rest\/find-reservations\?.*/).respond(function () {
    return [200, 'success', {}];
});
like image 31
gkalpak Avatar answered Oct 02 '22 20:10

gkalpak