I'm trying to create a mock object and use that within my Zend framework 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's throwing a PHPUnit_Framework_MockObject_BadMethodCallException exception, but not really much else (e.g. reason why). If I do a echo get_class($auth); exit;
from within my application I can see that it is of Mock_QA_Auth_f4627b7b
class, so at least it's picking up the mock instance. But when I call the isAuthenticated method, it throws that exception. What am I doing wrong?
Here is the error message I'm seeing:
$ ./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