我有一个完整的正在运行的应用程序,并想为它编写测试。我已经从Application
模块开始,我正在遵循手册。现在我正在编写“第一个控制器测试”并且正如预期的那样得到错误:
1) ApplicationTest\Controller\IndexControllerTest::testIndexActionCanBeAccessed
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Zend\Db\Adapter\Adapter
该错误表示服务管理器无法为我们创建数据库适配器的实例。我们间接使用数据库适配器
Album\Model\AlbumTable
从数据库中获取专辑列表。...
最好的办法是模拟我们
Album\Model\AlbumTable
从数据库中检索专辑列表的类。
当我现在按照手册进行操作时,我必须为每个测试方法或至少在每个测试方法中为我的应用程序的所有/许多表创建模拟setUp()
:
1) ApplicationTest\Controller\IndexControllerTest::testIndexActionCanBeAccessed
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for 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
这真的是推荐的方法吗?我应该在一个地方创建模拟然后以某种方式“包含”它们吗?不明白,一般如何处理。