gpt4 book ai didi

PHPUnit 测试双抛出 PHPUnit_Framework_MockObject_BadMethodCallException

转载 作者:可可西里 更新时间:2023-11-01 13:22:40 25 4
gpt4 key购买 nike

我正在尝试创建一个模拟对象并在测试时在我的 Zend 框架应用程序中使用它:

public function testAskQuestionRouteWithLoggedIn()
{
// get the mock auth object, and update the registry
$auth = $this->getMockBuilder('QA_Auth')
->disableOriginalConstructor()
->getMock();

// mock methods, and return values
$auth->method('isAuthenticated')
->will( $this->returnValue( true ) );

// update the registry
$auth = Zend_Registry::set('Auth', $auth);

// now preform the test as a logged in user
$this->dispatch('/ask');
$this->assertController('questions');
$this->assertAction('new');

// // // check the page contains a question form
$this->assertQueryCount('form#questionForm', 1);
}

...但它会抛出 PHPUnit_Framework_MockObject_BadMethodCallException 异常,但不会抛出其他异常(例如原因)。如果我执行 echo get_class($auth); exit; 从我的应用程序中我可以看到它属于 Mock_QA_Auth_f4627b7b 类,因此至少它正在获取模拟实例。但是当我调用 isAuthenticated 方法时,它会抛出该异常。我做错了什么?

这是我看到的错误消息:

$ ./vendor/bin/phpunit tests/application/controllers/QuestionsControllerTest.php 
PHPUnit 4.4.2 by Sebastian Bergmann.

Configuration read from /var/www/vhosts/qasystem/qasystem/tests/application/phpunit.xml

E

Time: 277 ms, Memory: 7.50Mb

There was 1 error:

1) QuestonsControllerTest::testAskQuestionRouteWithLoggedIn
PHPUnit_Framework_MockObject_BadMethodCallException:

/var/www/vhosts/qasystem/qasystem/application/controllers/BaseController.php:331
/var/www/vhosts/qasystem/qasystem/application/controllers/BaseController.php:29
/var/www/vhosts/qasystem/qasystem/application/controllers/QuestionsController.php:14
/var/www/vhosts/qasystem/qasystem/vendor/zendframework/zendframework1/library/Zend/Controller/Action.php:133
/var/www/vhosts/qasystem/qasystem/vendor/zendframework/zendframework1/library/Zend/Controller/Dispatcher/Standard.php:281
/var/www/vhosts/qasystem/qasystem/vendor/zendframework/zendframework1/library/Zend/Controller/Front.php:954
/var/www/vhosts/qasystem/qasystem/vendor/zendframework/zendframework1/library/Zend/Application/Bootstrap/Bootstrap.php:105
/var/www/vhosts/qasystem/qasystem/vendor/zendframework/zendframework1/library/Zend/Application.php:382
/var/www/vhosts/qasystem/qasystem/tests/application/controllers/BaseControllerTestCase.php:67
/var/www/vhosts/qasystem/qasystem/tests/application/controllers/QuestionsControllerTest.php:26

最佳答案

QA_Auth::isAuthenticated() 是静态方法,不能模拟静态方法。

Limitation: final, private, and static methods

Please note that final, private and static methods cannot be stubbed or mocked. They are ignored by PHPUnit's test double functionality and retain their original behaviour.

Test Doubles

手册说测试替身“保留其原始行为”,但对于静态方法而言并非如此。有一个 open issue about it .另见 PHPUnit Mock Objects and Static Methods .

关于PHPUnit 测试双抛出 PHPUnit_Framework_MockObject_BadMethodCallException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28454506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com