使用 PHPUnit 测试 ZF 控制器的受保护方法

发布于 2024-11-13 19:15:41 字数 2119 浏览 6 评论 0原文

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

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

我的测试(遵循这篇文章)看起来像这样:

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

我还尝试模拟具有相同结果的类。有什么想法吗?

谢谢

I want to test a protected method which is inside a controller:

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

My test (following this post) look like 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);
}

When I execute the test, I got this runtime exception

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

I also tried to mock the class with same results. Any ideas?

Thanks

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

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

发布评论

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

评论(1

此生挚爱伱 2024-11-20 19:15:41

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

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

You need to pass in a request and response to the controller's constructor.

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