unit testing - Is it necessary to create mocks for all table classes in every PHPUnit test method in Zend Framework 2? -
i have complete running application , want write tests it. i've started application
module , following manual. i'm writing "first controller test" , expected getting error:
1) applicationtest\controller\indexcontrollertest::testindexactioncanbeaccessed zend\servicemanager\exception\servicenotfoundexception: zend\servicemanager\servicemanager::get unable fetch or create instance zend\db\adapter\adapter
the error says service manager can not create instance of database adapter us. database adapter indirectly used our
album\model\albumtable
fetch list of albums database....
the best thing mock out our
album\model\albumtable
class retrieves list of albums database.
when i'd follow manual now, i'd have create mocks / many tables of application in every test method or @ least in every setup()
:
1) applicationtest\controller\indexcontrollertest::testindexactioncanbeaccessed zend\servicemanager\exception\servicenotfoundexception: zend\servicemanager\servicemanager::get unable fetch or create instance zend\db\adapter\adapter /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:456 /path/to/project/module/catalog/module.php:56 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:737 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:869 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:494 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:441 /path/to/project/module/catalog/module.php:51 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:737 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:869 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:494 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:441 /path/to/project/module/cache/module.php:58 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:737 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:869 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:494 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:441 /path/to/project/module/search/module.php:61 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:737 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:869 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:494 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:441 /path/to/project/module/search/module.php:81 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:737 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/abstractpluginmanager.php:205 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:494 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/servicemanager.php:441 /path/to/project/vendor/zendframework/zendframework/library/zend/servicemanager/abstractpluginmanager.php:103 /path/to/project/vendor/zendframework/zendframework/library/zend/view/renderer/phprenderer.php:378 /path/to/project/vendor/zendframework/zendframework/library/zend/view/renderer/phprenderer.php:397 /path/to/project/module/application/view/layout/layout.phtml:76 /path/to/project/module/application/view/layout/layout.phtml:76 /path/to/project/vendor/zendframework/zendframework/library/zend/view/renderer/phprenderer.php:507 /path/to/project/vendor/zendframework/zendframework/library/zend/view/view.php:205 /path/to/project/vendor/zendframework/zendframework/library/zend/mvc/view/http/defaultrenderingstrategy.php:126 /path/to/project/vendor/zendframework/zendframework/library/zend/eventmanager/eventmanager.php:472 /path/to/project/vendor/zendframework/zendframework/library/zend/eventmanager/eventmanager.php:207 /path/to/project/vendor/zendframework/zendframework/library/zend/mvc/view/http/defaultrenderingstrategy.php:136 /path/to/project/vendor/zendframework/zendframework/library/zend/eventmanager/eventmanager.php:472 /path/to/project/vendor/zendframework/zendframework/library/zend/eventmanager/eventmanager.php:207 /path/to/project/vendor/zendframework/zendframework/library/zend/mvc/application.php:332 /path/to/project/vendor/zendframework/zendframework/library/zend/mvc/application.php:307 /path/to/project/vendor/zendframework/zendframework/library/zend/test/phpunit/controller/abstractcontrollertestcase.php:255 /path/to/project/module/application/test/applicationtest/controller/indexcontrollertest.php:41
can recomended approach? should create mocks in 1 place , "include" them somehow? don't get, how handle in general.
you create mock of objects need inject in test. in example, create mock of dbtable can control database interaction , not dependent on setting sort of database (making test more brittle).
you need services required testing specific action in controller. each of actions should not interacting many services each test should have couple of mocks in in order run.
Comments
Post a Comment