PHPUnit test double throws PHPUnit_Framework_MockObject_BadMethodCallException

I am trying to create a mock object and use it in my Framework Zend application when testing:

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); } 

... but it throws a PHPUnit_Framework_MockObject_BadMethodCallException exception, but not quite another (like why). If I do echo get_class($auth); exit; echo get_class($auth); exit; from my application, I see that it has the Mock_QA_Auth_f4627b7b class, so at least it Mock_QA_Auth_f4627b7b mock instance. But when I call the isAuthenticated method, it throws this exception. What am I doing wrong?

Here is the error message that I see:

 $ ./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 
+6
source share
1 answer

QA_Auth::isAuthenticated() is a static method, static methods cannot be mocked.

Limitation: final, private, and static methods

Please note that final, private, and static methods cannot be trimmed or mocked. They are ignored by the dual functionality of PHPUnit and retain their original behavior.

- Test Doubles

The manual says that the test doubles "keep its original behavior", but this is not so for static methods. There is an open problem. Also see PHPUnit Mock Objects and static methods .

+4
source

Source: https://habr.com/ru/post/982320/


All Articles