我有一个这样的测试方法:
public function testTaskPut()
{
$this->dispatch('/task/67', 'put');
$this->assertResponseStatusCode(200);
$this->assertModuleName('Task');
$this->assertControllerName('Task\Controller\Task');
$this->assertControllerClass('TaskController');
$this->assertMatchedRouteName('task');
}
现在我想设置原始帖子内容:
{"content":"example content"}
怎么做?
我可以这样做:
$request = new Request();
$request->setContent(json_encode(array(
'content' => 'example content'
)));
但如何在dispatch()中使用$request
答案 0 :(得分:0)
我没有对此进行过测试,但是ZF测试控制器类已经有一个用于调度的请求对象,所以你应该可以这样做:
public function testTaskPut()
{
$request = $this->getRequest();
$request->setContent(json_encode(array(
'content' => 'example content'
)));
$this->dispatch('/task/67', 'put');
$this->assertResponseStatusCode(200);
$this->assertModuleName('Task');
$this->assertControllerName('Task\Controller\Task');
$this->assertControllerClass('TaskController');
$this->assertMatchedRouteName('task');
}
然后当您致电dispatch()
时,应使用您的数据。