【问题标题】:testing with phpunit使用 phpunit 进行测试
【发布时间】:2011-10-10 12:37:57
【问题描述】:

我正在尝试测试我的控制器。 我遵循了这个指南:

http://www.zendcasts.com/unit-testing-a ... s/2010/11/

一切都很好,直到我想在我的测试函数中使用这个调用: $this->dispatch('/'); 我认为问题在于引导,但我不知道如何解决这个问题。 我粘贴了我参与测试的文件:

application.ini = http://pastie.org/2248669

phpunit.xml =

<phpunit bootstrap="./application/bootstrap.php" colors="true">
<testsuite name="Application Test Suite">
    <directory>./application</directory>
</testsuite>
<testsuite name="Library Test Suite">
    <directory>./library</directory>
</testsuite>

<filter>
    <!-- If Zend Framework is inside your project's library, uncomment this filter -->
    <whitelist>
  <directory suffix=".php">../application</directory>
  <directory suffix=".php">../library/Kolcms</directory>
    <exclude>
      <directory suffix=".php">../library/Zend</directory>
      <directory suffix=".php">../library/Kolcms/Model/DB</directory>
      <directory suffix=".phtml">../library/</directory>
      <directory suffix=".phtml">../application/</directory>
      <file suffix=".php">../application/Bootstrap.php</file>
    </exclude>
    </whitelist>

</filter>

<logging>
  <log type="coverage-html" target="./log/report" charset="UTF-8"
   yui="true" highlight = "true" lowUpperBound="50" highLowerBound="80" />
</logging>

bootstrap.php =

   <?php

  // Define path to application directory
defined('APPLICATION_PATH')
   || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));

// Define application environment
define('APPLICATION_ENV', 'testing');

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
   realpath(APPLICATION_PATH . '/../library'),
   realpath(APPLICATION_PATH . '/../../../Zend'),
   get_include_path()
)));


/** Zend_Application */
require_once 'Zend/Application.php';
require_once 'ControllerTestCase.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
   APPLICATION_ENV,
   APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();
clearstatcache();

ControllerTestCase.php =

<?php
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

class ControllerTestCase
extends Zend_Test_PHPUnit_ControllerTestCase
{

/**
 *
 * @var Zend_Application
 */

protected $application;

public function setUp()
{
    $this->bootstrap = array($this,'appBootstrap');
    parent::setUp();
}
public function appBootstrap()
{
    $this->application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
            );

$this->application->bootstrap();

}

public function  tearDown()
{
  $this->resetRequest();
  $this->resetResponse();

  parent::tearDown();
}

}

indexControllerTest.php =

<?php
include_once APPLICATION_PATH . '/modules/core/controllers/IndexController.php';
class Core_IndexControllerTest extends ControllerTestCase
{
      public function testDefaultShouldInvokeAction()
      {
          $this->dispatch('/core/index/index');
          $this->assertController('index');
          $this->assertAction('index');
          $this->assertModule('core');
      }
}

这是我的 phpunit 输出:

$ phpunit 
PHPUnit 3.5.5 by Sebastian Bergmann.

PHP Fatal error: Call to a member function hasResource() on a non-object in  
/home/aykut/dev/kolonicms/application/modules/core/controllers/ErrorController.php on 
line 49

Fatal error: Call to a member function hasResource() on a non-object in 
/home/aykut/dev/kolonicms/application/modules/core/controllers/ErrorController.php
on line 49

请有人帮助我。 谢谢

【问题讨论】:

    标签: php zend-framework controller phpunit


    【解决方案1】:

    我的猜测是您没有 URL /core/index/index 的路由,因此 ZF 正在重定向到您的错误控制器。接下来,ErrorController.php 有一个错误,导致它在不包含对象或为null 的变量上调用hasResource() 方法。如果您先修复错误控制器,可能会更容易诊断问题。

    这正是我为控制器和视图构建单独的抽象测试用例类的原因。我不喜欢 Zend 的 ControllerTestCase 除了控制器之外还测试路由、调度程序和视图。

    【讨论】:

      【解决方案2】:

      如果我们可以看到dispatch() 方法中的代码,那就更容易了,但我可以假设setUp() 中某处可能触发了错误(可能在引导程序中),吞下了异常.之后它尝试调用tearDownresetRequest() 中使用的属性(可能是$this-&gt;application$this-&gt;bootstrap,在设置中从未初始化,导致PHP 错误。

      我发布了answer 解释何时会发生此类错误或错误。我知道这是一个旧帖子,但如果它可以帮助其他人。

      【讨论】:

        猜你喜欢
        • 2016-06-04
        • 2011-06-02
        • 2015-09-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-25
        • 2017-08-14
        • 2014-12-25
        • 2015-08-25
        相关资源
        最近更新 更多