我正在尝试将 url 与正则表达式匹配。但是下面的正则表达式不匹配。
$httpBackend.whenGET('/^rest\/find-reservations\?.*/)').respond(function () {
return [200, ['succes'], {}];
});
我不断收到以下错误:
Error: Unexpected request: GET rest/find-reservations?end=1421424299193&reservationClass=Reservation&start=1358352299193
No more request expected
当我将正则表达式更改为绝对字符串 '/find-reservations' 时,会触发 whenGET。为什么是这样?
编辑:
我在 mock 一个后端。 http://plnkr.co/edit/VF4KbZO3FvngWQsiUcte?p=preview
以下 plnkr 适用于静态 url 和部分。但在上述情况下并非如此。
最佳答案
如果你想匹配这个 url:
“休息/查找预订?结束=1421424299193&reservationClass=预订&开始=1358352299193”
使用此代码:
$httpBackend.whenGET(/^rest\/find-reservations\?.*/).respond(function () {
return [200, ['success'], {}];
});
如果您看到此错误
Error: Unexpected request:
这可能是由于某些原因:
$httpBackend.expectGET(url)
. expectGET
的顺序应该与 requests
的顺序相同. $httpBackend.verifyNoOutstandingExpectation()
之前 $httpBackend.flush()
. 与
$httpBackend.whenGET
无关一点也不。来自 $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
关于angularjs - 模拟 $httpBackend 正则表达式不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21169076/