PHPUnit 配置测试

发布于 2024-11-05 07:51:07 字数 639 浏览 11 评论 0原文

class ControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    public $object;

    public function  setUp() {
        $this->bootstrap = array($this, 'boostrap');
        parent::setUp();
    }

    public function bootstrap(){
        $this->application = new Zend_Application(
                    APPLICATION_ENV,
                    APPLICATION_PATH . '/configs/application.ini'
                );
        $this->application->bootstrap();

    }

    public function testIndexAction(){
        // body
    }

}

这是测试的班级。我的问题是如何实现 testIndexAction,其中命令提示符上的实际命令是:

php zfrun.php -a ..index

class ControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    public $object;

    public function  setUp() {
        $this->bootstrap = array($this, 'boostrap');
        parent::setUp();
    }

    public function bootstrap(){
        $this->application = new Zend_Application(
                    APPLICATION_ENV,
                    APPLICATION_PATH . '/configs/application.ini'
                );
        $this->application->bootstrap();

    }

    public function testIndexAction(){
        // body
    }

}

This is the class for the test. My question is how to implement the testIndexAction where the actual command on the command prompt is:

php zfrun.php -a ..index

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

束缚m 2024-11-12 07:51:07

在没有看到 zfrun.php 的具体用途的情况下,我只能猜测,而且听起来您需要放弃 ControllerTestCaseControllerTestCase 旨在模拟通过 Zend 调度程序发送的 HTTP 请求,但您不需要它。

相反,您可以尝试通过设置 $argv 来模拟从命令行调用 zfrun.php ,就像它看起来一样并执行 zfrun.php

function testIndexAction() {
    $argv = array(
            '-a',
            'module_name.controller_name.index',
        );
    require 'zfrun.php';
}

问题是,这个仅适用于一次测试,假设 zfrun.php 定义了类或函数并且不能多次需要。因此,您需要在新的测试用例基类中执行 zfrun.php 所做的任何事情,而不使用 zfrun.php 本身。本质上将其代码重构为可重用的测试辅助方法。

function executeControllerAction($module, $controller, $action) {
    ... whatever magic zfrun.php does ...
}

Without seeing exactly what zfrun.php does, I can only guess, and it sounds like you need to ditch ControllerTestCase. ControllerTestCase is designed to mimic an HTTP request to send through the Zend dispatcher, but you don't need that.

Instead, you can try to mimic calling zfrun.php from the command line by setting up $argv as it would look and executing zfrun.php yourself:

function testIndexAction() {
    $argv = array(
            '-a',
            'module_name.controller_name.index',
        );
    require 'zfrun.php';
}

The problem is that this works for only one test, assuming zfrun.php defines classes or functions and cannot be required multiple times. Therefore, you'll need to do whatever zfrun.php does in a new test case base class without using zfrun.php itself. Essentially refactor its code into a reusable test helper method.

function executeControllerAction($module, $controller, $action) {
    ... whatever magic zfrun.php does ...
}
顾铮苏瑾 2024-11-12 07:51:07

如果这是对您的主页的测试,请使用“

$this->dispatch('/');

如果不是,您必须为其提供将触发加载此控制器的路由的 URL。

If this is the test for your home page, use

$this->dispatch('/');

If not, you'll have to give it the URL that will trigger the route to load this controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文