嗨,我是Zend的新手,并被要求与Z2一起开发.我试图通过控制器插件添加可重用的功能,但我没有成功进行单元测试.它在常规应用程序中工作正常.
// Application\Controller\Plugin\HelloWorld.php
namespace Application\Controller\Plugin;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Http\Client;
use Zend\Http\Request;
class HelloWorld extends AbstractPlugin
{
public function helloWorld()
{
return "HELLO WORLD";
}
}
// Application\Controller\IndexController.php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
echo $this->helloworld()->helloWorld();
}
}
//Application\config\module.config.php
...
'controller_plugins' => array(
'invokables' => array(
'helloworld' => 'Application\Controller\Plugin\HelloWorld',
),
),
...
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for helloworld
Run Code Online (Sandbox Code Playgroud)
Jur*_*man 12
如果为控制器创建单元测试,则可以在专用的受控单元中测试控制器.您没有初始化应用程序,您没有加载模块,也没有解析完整的配置文件.
要对控制器进行单元测试,请在setUp()方法中自行添加插件,以便将其作为可调用方式直接添加到服务管理器中.如果要测试配置是否有效,则需要考虑功能测试.尝试首先引导整个应用程序,然后通过创建请求并断言响应来测试控制器.
因为功能测试有点难以解决,所以更容易从控制器(插件)开始在单元测试中进行测试:
namespace SlmLocaleTest\Locale;
use PHPUnit_Framework_TestCase as TestCase;
use Application\Controller\IndexController;
class IndexControllerTest extends TestCase
{
public function setUp()
{
$controller = new IndexController;
$controller->getPluginManager()
->setInvokableClass('helloworld', 'Application\Controller\Plugin\HelloWorld');
$this->controller = $controller;
}
public function testCheckSomethingHere()
{
$response = $this->controller->indexAction();
}
}
Run Code Online (Sandbox Code Playgroud)
您可以替换setInvokableClass()通过setService()注入例如模拟.
| 归档时间: |
|
| 查看次数: |
1819 次 |
| 最近记录: |