gpt4 book ai didi

zend-framework - Zend 框架 : How to do unit testing in case of remote API calls

转载 作者:行者123 更新时间:2023-12-05 01:33:10 25 4
gpt4 key购买 nike

我正在为我的一个大型 Zend Framework 应用程序编写一些集成测试,使用 Zend_Test。但我一直不知道如何为我的一些使用定制 Web API 的 Controller 编写测试。我知道 Zend_Controller_Response_HttpTestCase,ZF 手册指出它在这里可能会有帮助,但我发现文档真的很少。

如何在不调用远程服务器的情况下编写测试?最佳实践?如果 Zend_Controller_Response_HttpTestCase 被调用,那么我该如何使用它呢?这是我想在其中使用的典型测试方法:

class FooControllerTest extends ControllerTestCase {

public function testMyNiftyFooPage() {
$this->dispatch('/foo'); // a page that calls a remote API

$this->assertQueryContentContains('h1', 'Hello World');
// other assertions, etc.
}
}

最佳答案

将处理远程调用的逻辑封装在一个类中,让您的 Controller 接受它的一个实例(这称为依赖注入(inject))和mock用于测试。

这就是通常进行测试的方式 - 如果您正在编写火箭发射器软件,则不应在每次运行测试时都发射核弹。

关于zend-framework - Zend 框架 : How to do unit testing in case of remote API calls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7074274/

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