angularjs - 模拟 $httpBackend 正则表达式不匹配

标签 angularjs

我正在尝试将 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/

    相关文章:

    javascript - 在 Angular karma 测试中模拟 Stripe

    javascript - Angular/ ionic : Keep all text in a separate file

    javascript - 条件 ng-click 中的函数和变量赋值

    javascript - AngularJS Controller 中使用 as 语法的服务 promise

    javascript - 将对象从函数返回到 AngularJS 中的 ng-repeat

    javascript - 在服务中使用 promise 但不返回 promise

    javascript - Angular 多列 orderby 将数字转换为字符串

    javascript - 使用来自 json 的 ng-flow.js 填充图像文件

    javascript - 检查项目是否在列表中(指令内)

    javascript - 如何将现有属性及其值替换为自定义指令并在指令中调用函数