Ame*_*lie 7 php phpunit zend-db zend-framework2
使用PHPUnit创建测试时遇到一些问题.
这是我的设置:
protected function setUp()
{
$serviceManager = Bootstrap::getServiceManager();
$this->mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
$this->mockConnection = $this->getMock('Zend\Db\Adapter\Driver\ConnectionInterface');
$this->mockDriver->expects($this->any())->method('checkEnvironment')->will($this->returnValue(true));
$this->mockDriver->expects($this->any())->method('getConnection')->will($this->returnValue($this->mockConnection));
$this->mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface');
$this->mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$this->mockDriver->expects($this->any())->method('createStatement')->will($this->returnValue($this->mockStatement));
$this->adapter = new Adapter($this->mockDriver, $this->mockPlatform);
$this->sql = new Sql($this->adapter);
$mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array(), array(), '', false);
$maiFormuleRevisionTable = $this->getMockBuilder('Maintenance\Model\BDD\PMaiFormulerevisionTable')
->setMethods(array())
->setConstructorArgs(array($mockTableGateway, $this->adapter, $this->sql))
->getMock();
$maiFormulerevisionService = $this->getMockBuilder('Maintenance\Service\Model\PMaiFormulerevisionService')
->setMethods(array())
->setConstructorArgs(array($maiFormuleRevisionTable))
->getMock();
$this->assertTrue($maiFormulerevisionService instanceof PMaiFormulerevisionService);
$this->controller = new RevisionsController($maiFormulerevisionService);
$this->request = new Request();
$this->routeMatch = new RouteMatch(array('controller' => 'index'));
$this->event = new MvcEvent();
$config = $serviceManager->get('Config');
$routerConfig = isset($config['router']) ? $config['router'] : array();
$router = HttpRouter::factory($routerConfig);
$this->event->setRouter($router);
$this->event->setRouteMatch($this->routeMatch);
$this->controller->setEvent($this->event);
$this->controller->setServiceLocator($serviceManager);
}
Run Code Online (Sandbox Code Playgroud)
这是我的功能测试:
public function testEditFormuleActionCanBeAccessed()
{
$this->routeMatch->setParam('action', 'loadformule');
$this->routeMatch->setParam('idformule', '23');
$result = $this->controller->dispatch($this->request);
$response = $this->controller->getResponse();
$this->assertEquals(200, $response->getStatusCode());
}
Run Code Online (Sandbox Code Playgroud)
我的控制器:
public function loadformuleAction()
{
try {
$iStatus = 0;
$iMaiFormuleRevisionId = (int) $this->params('idformule');
$oFormule = $this->maiFormulerevisionService->selectByIdOrCreate($iMaiFormuleRevisionId);
$maiFormulerevisionForm = new PMaiFormulerevisionForm($oFormule);
if ($this->getRequest()->isPost()) {
/* etc ... */
}
$viewModel = new ViewModel();
$viewModel->setTerminal(true);
$viewModel->setVariables([
'maiFormulerevisionForm' => $maiFormulerevisionForm,
'iMaiFormuleRevisionId' => $oFormule->getMaiFormuleRevisionId(),
'iStatus' => $iStatus
]);
return $viewModel;
} catch (\Exception $e) {
throw new \Exception($e);
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试运行我的测试时,它显示一个错误,我指出我的测试不会进入我的服务,当我调用它时($ this-> maiFormulerevisionService):
1)MaintenanceTest\Controller\RevisionsControllerTest :: testEditFormuleActionCanBeAccessed异常:传递给Maintenance\Form\PMaiFormulerevisionForm :: __ construct()的参数1必须是Maintenance\Model\PMaiFormulerevision的实例,给出null
我不明白为什么我的模拟不起作用......
谢谢你的回答:)
编辑:
哼......我试试的时候:
$maiFormulerevisionService = new PMaiFormulerevisionService($maiFormuleRevisionTable);
Run Code Online (Sandbox Code Playgroud)
而不是这个:
$maiFormulerevisionService = $this->getMockBuilder('Maintenance\Service\Model\PMaiFormulerevisionService')
->setMethods(array())
->setConstructorArgs(array($maiFormuleRevisionTable))
->getMock();
Run Code Online (Sandbox Code Playgroud)
它进入服务但不进入服务构造函数($ maiFormuleRevisionTable)中指定的TableGateway ...所以它仍然不起作用...
您设置了一个模拟,但您还必须设置调用该方法时模拟返回的内容selectByIdOrCreate。既然你这样做了:
$oFormule = $this->maiFormulerevisionService->selectByIdOrCreate($iMaiFormuleRevisionId);
$maiFormulerevisionForm = new PMaiFormulerevisionForm($oFormule);
Run Code Online (Sandbox Code Playgroud)
只要您没有为此方法设置返回值,模拟就会返回该null方法。selectByIdOrCreate
尝试添加这样的模拟方法:
$mock = $maiFormulerevisionService;
$methodName = 'selectByIdOrCreate';
$stub = $this->returnValue($maiFormuleRevisionTable);
$mock->expects($this->any())->method($methodName)->will($stub);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
239 次 |
| 最近记录: |