$httpBackend 不接受正则表达式

$httpBackend not accepting regexp

在我的应用程序中,将在 http://localhost:8888/api/lists 上进行调用。我可以通过使用以下调用来验证是否进行了此调用:

$httpBackend.whenGET("http://localhost:8888/api/lists").respond(200, dataMock);
$httpBackend.expectGET("http://localhost:8888/api/lists");

这些测试通过得很好,$httpBackend.verifyNoOutstandingExpectation() 也会通过。但是,我希望能够配置调用我的产品的域 API。因此,我尝试切换到以下内容:

$httpBackend.whenGET("/\/api\/lists/").respond(200, dataMock);
$httpBackend.expectGET("/\/api\/lists/");

然后测试用例将失败并显示消息 "Error: No pending request to flush !"。我已经阅读了 API 以及 this 之类的问题,但我无法弄清楚为什么它在我的案例中不起作用。有人可以给我一些指示吗?

您将正则表达式括在双引号内,使其成为普通字符串。您应该删除它们,因此,它将如下所示:

$httpBackend.whenGET(/\/api\/lists/).respond(200, dataMock);
$httpBackend.expectGET(/\/api\/lists/); 

只需从 Regex 中删除引号。