【问题标题】:Test protected methods of ZF controllers with PHPUnit使用 PHPUnit 测试 ZF 控制器的受保护方法
【发布时间】:2011-09-09 23:02:34
【问题描述】:

我想测试控制器内部的受保护方法:

class AuthenticateController extends Zend_Controller_Action
{
    protected function getAuthAdapter(){}
}

我的测试(在this 发布之后)如下所示:

public function testGetAuthAdapterShouldReturnZendAuthAdapterDbTable()
{
    require_once(APPLICATION_PATH.'/controllers/AuthenticateController.php');
    $method = new Zend_Reflection_Method('AuthenticateController', 
                                         'getAuthAdapter');
    $class = new AuthenticateController();
    $result = $method->invoke($class);

    // assert result value
    $this->assertInstanceOf('Zend_Auth_Adapter_DbTable', $result);
}

当我执行测试时,我得到了这个运行时异常

RuntimeException: Fatal error: Call to a member function getModuleName() on a non-object in /Users/padawan_rodrigo/Documents/workspace/ZendFramework-1.11.5/library/Zend/Controller/Action/Helper/ViewRenderer.php on line 226
Call Stack:
0.0030     396924   1. {main}() -:0
0.0755    4593044   2. __phpunit_run_isolated_test() -:207
0.0771    4718524   3. PHPUnit_Framework_TestCase->run() -:22
0.0785    4798460   4. PHPUnit_Framework_TestResult->run() /usr/local/pear/share/pear/PHPUnit/Framework/TestCase.php:576
0.3565    4827948   5. PHPUnit_Framework_TestCase->runBare() /usr/local/pear/share/pear/PHPUnit/Framework/TestResult.php:666
0.6696   10508956   6. PHPUnit_Framework_TestCase->runTest() /usr/local/pear/share/pear/PHPUnit/Framework/TestCase.php:628
0.6697   10509780   7. ReflectionMethod->invokeArgs() /usr/local/pear/share/pear/PHPUnit/Framework/TestCase.php:738
0.6697   10509796   8. AuthenticateControllerTest->testGetAuthAdapterShouldReturnZendAuthAdapterDbTable() /project/code/tests/application/controllers/AuthenticateControllerTest.php:0
0.6794   10753940   9. Zend_Controller_Action->__construct() /project/code/tests/application/controllers/AuthenticateControllerTest.php:172

我还尝试以相同的结果模拟课程。有什么想法吗?

谢谢

【问题讨论】:

标签: zend-framework reflection phpunit controllers


【解决方案1】:

您需要将请求和响应传递给控制器​​的构造函数。

$class = new AuthenticateController(
        new Zend_Controller_Request_HttpTestCase(), 
        new Zend_Controller_Response_HttpTestCase()
    );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2011-06-27
    相关资源
    最近更新 更多