【发布时间】:2015-01-14 09:38:19
【问题描述】:
我正在测试这个功能
/**
* @Route("/list", name="_clients")
* @Method("GET")
*/
public function ClientsAction()
{
$em = $this->getDoctrine()->getManager();
$data = $em->getRepository('InvoiceBundle:Clients')->findByUser($this->user());
if($data){
$Clients = array();
foreach($data as $v){
if($v->getCompanyId() != 0 ) {
$companyId = $v->getCompanyId();
} else {
$companyId = '';
}
if ($v->getClient() == 'person'){
$company = $v->getName().' '.$v->getLname();
} else {
$company = $v->getCompany();
}
$Clients[] = array(
'id' => $v->getId(),
'settings' => $company,
'companyId' => $companyId,
'client' => $v->getClient(),
'mobile' => $v->getMobile(),
'email' => $v->getEmail(),
'clientName' => $v->getClientName(),
'delivery' => $v->getDelivery(),
'ContactPerson' => $v->getContactPerson()
);
}
} else {
$Clients = array('data' => 'empty');
}
$response = new JsonResponse($Clients);
return $response;
}
它自己的功能正确运行,但是我想用这个功能检查我的'Content-Type'是否是Json
public function testClients()
{
$client = static::createClient();
$client->request('GET', '/clients/list');
$this->assertTrue(
$client->getResponse()->headers->contains(
'Content-Type',
'application/json'
)
);
}
这样我得到一个 FALSE 值。 然后我尝试对状态代码进行测试
$this->assertSame(200, $client->getResponse()->getStatusCode());
这样我得到错误 500 而不是 200 OK 我明白这就是为什么我在“内容类型”测试中得到 FALSE 值但我不明白为什么。 我根据 Symfony 文档做这一切。 可能是我做错了什么还是只是您无法检查“内容类型”?
任何帮助将不胜感激!
【问题讨论】:
-
愚蠢的问题:网址正确吗?
标签: php json unit-testing symfony content-type