【问题标题】:Zend Framework UnitTestZend 框架单元测试
【发布时间】:2013-03-20 03:32:35
【问题描述】:

我尝试使用 Zend_Test_PHPUnit 为我的应用程序编写单元测试,但我总是得到 ​​p>

1) IndexControllerTest::testValidation
Failed asserting last controller used <"error"> was "test"

我已经创建了一个测试控制器,但即使在那里我也无法让它工作。

谁能帮忙?

谢谢!

class TestController extends Zend_Controller_Action
{

   public function indexAction()
   {
        print 'test';
   }

}

引导是:

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

 // Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));

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

require_once 'Zend/Loader/Autoloader.php'; Zend_Loader_Autoloader::getInstance();

phpunit.xml 是:

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

    <filter>
    <whitelist>
        <directory suffix=".php">../library/</directory>
        <directory suffix=".php">../application/</directory>
        </whitelist>
    </filter>
</phpunit>

测试控制器是

class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{

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

    public function testValidation()
    {
         $this->dispatch('/test/');
         $this->assertController("test");
    }
}

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    您的 TestController 中似乎有错误。

    您的视图是否可用 (/test/index.phtml)?

    好的解决方案是检查抛出的异常(将单元测试包装在 Try/Catch 块中并打印到错误日志)。

    【讨论】:

    • 这是缺少的视图。我只创建了一个 JSON-API,这就是我不需要视图的原因。谢谢!
    【解决方案2】:

    我得到了完全相同的错误,但由于完全不同的问题。

    更通用的解决方案可能是将其添加到测试方法中

        print_r(strip_tags(
            $this->bootstrap
                 ->getBootstrap()
                 ->getResource('layout')
                 ->content
        )); die;
    

    这是假设 Zend_Layout 正在被使用...它打印出 error.phtml 文件的内容,所以至少你可以确切地看到发生了什么。

    您应该会看到如下内容:

    An error occurred 
    Application error 
    
    Exception information: 
    
        Message: {Your Error Message will appear here}
    
    
    Stack trace: 
        {A stack trace will follow...}
    

    在“消息:”之后出现您的特定错误消息,然后在“堆栈跟踪:”之后出现完整的堆栈跟踪。

    希望这至少有助于调试潜在问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 2010-09-10
      • 2023-04-10
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多