0

我有一个完整的正在运行的应用程序,并想为它编写测试。我已经从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

这真的是推荐的方法吗?我应该在一个地方创建模拟然后以某种方式“包含”它们吗?不明白,一般如何处理。

4

1 回答 1

0

您只需要在该测试中注入的对象创建一个模拟。在示例中,您创建了 DbTable 的模拟,以便您可以控制数据库交互并且不依赖于设置任何类型的数据库(使测试更加脆弱)。

您需要为测试控制器中的特定操作所需的任何服务执行此操作。您的每个操作都不应该与太多服务交互,因此每个测试应该只有几个模拟才能运行。

于 2013-05-09T03:07:54.313 回答