如何从 $httpBackend whenGET 获取正则表达式值

How to get regex value from $httpBackend whenGET

  $httpBackend.whenGET(/api\/product\/[a-zA-Z0-9]{6}/).respond(function(data) {
      // Code to find given ID in collection
  });

当我调用此端点时,我应该 return 匹配给定产品 ID(正则表达式值)的产品。我如何访问这个值?数据值刚好等于'GET'.

我假设您在单元测试中使用 $httpBackend。如果是这样,你有两个选择。您可以选择使用 respond 来定义自定义响应(这就是您在上面所做的)。

你可以使用 passThrough 方法,它实际上应该调用 API (这会使你的测试稍微不稳定,因为它们不是自力更生的,服务中断可能导致测试失败。

您可以在 AngularJS $httpBackend documentation.

上阅读有关 .get(包括 whenGET)和 .passThrough 方法的信息

编辑

$httpBackend.whenGET(/api\/product\/[a-zA-Z0-9]{6}/)
    .respond(function(method, url, data, headers) {
        /** do something with url */
    });