gpt4 book ai didi

javascript - Angular 防止 mock $http 服务

转载 作者:行者123 更新时间:2023-12-03 12:19:52 25 4
gpt4 key购买 nike

我正在为连接到 REST API 的 Angular 应用程序编写一些测试,我们也在开发该应用程序。在本例中,我实际上想要进行集成测试而不是单元测试,因此我不想模拟 $http 服务。我想做一个真正的 AJAX 请求并测试响应是否符合我们的预期。

我不能简单地删除 angular-mock.js 因为这是其他测试所必需的,有没有办法强制 Angular 使用真实的服务而不是替换为模拟的服务?

最佳答案

您是否尝试过使用“passThrough”选项?它是专门为此目的而设计的:

As opposed to unit-testing, in an end-to-end testing scenario or in scenario when an application is being developed with the real backend api replaced with a mock, it is often desirable for certain category of requests to bypass the mock and issue a real http request (e.g. to fetch templates or static files from the webserver). To configure the backend with this behavior use the passThrough request handler of when instead of respond.

例如:

$httpBackend.whenGET(/my-resource\/.*/).passThrough();

关于javascript - Angular 防止 mock $http 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24493924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com