模拟$ httpBackend正则表达式不匹配 [英] mock $httpBackend regex not matched
问题描述
我想匹配一个正则表达式的URL。但follwing正则表达式不匹配。
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'], {}];
});
我不断收到follwing错误:
I keep getting the follwing error:
Error: Unexpected request: GET rest/find-reservations?end=1421424299193&reservationClass=Reservation&start=1358352299193
No more request expected
当我改变正则表达式为绝对字符串'/找到-保留'的whenGET被触发。这是为什么?
When I change the regex to an absolute string '/find-reservations' the whenGET is triggered. Why is this?
编辑:
我嘲笑后端。 <一href=\"http://plnkr.co/edit/VF4KbZO3FvngWQsiUcte?p=$p$pview\">http://plnkr.co/edit/VF4KbZO3FvngWQsiUcte?p=$p$pview
下面plnkr工作正常静态URL和谐音。但它不确实在上述情况下
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.
推荐答案
如果要匹配这个网址:
\"rest/find-reservations?end=1421424299193&reservationClass=Reservation&start=1358352299193\"
使用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:
- 您忘记
$ httpBackend.expectGET(URL)
。 - 的顺序
expectGET
应该是一样的要求
的顺序。 - 您名为
$ httpBackend.verifyNoOutstandingExpectation()
在$ httpBackend.flush()
。
- You forget
$httpBackend.expectGET(url)
. - The order of
expectGET
should be the same as the order of therequests
. - You called
$httpBackend.verifyNoOutstandingExpectation()
before$httpBackend.flush()
.
这是不是在所有相关的 $ httpBackend.whenGET
。
It is not related to $httpBackend.whenGET
at all.
请求期望提供一种方法,使有关应用程序的请求断言,并定义这些请求的响应。 测试将失败,如果预期的请求没有做出或他们是在错误的顺序做
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
这篇关于模拟$ httpBackend正则表达式不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!