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.
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'], {}];
});
Error: Unexpected request:
It can be for some reasons:
$httpBackend.expectGET(url)
.expectGET
should be the same as the order of the requests
.$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
There are two problems with your code:
Enclosing an expression in '...'
makes it a string (even if it looks like a regular expression.
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', {}];
});
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