【问题标题】:Symfony PHPUnit Test ( error 500 )Symfony PHPUnit 测试(错误 500)
【发布时间】: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


【解决方案1】:

JsonResponse 确实添加了 Content-Type 标头(应用程序/json),因此这应该不是问题。

我认为主要问题是您在client->request() 行上缺少$

编辑:

在你的班级声明之前,你是否添加了@Route("/clients")

或者,findByUser 返回的数据可能不是您所期望的,调用$v 失败。

【讨论】:

  • 抱歉复制错了它有$但还是同样的问题
猜你喜欢
  • 2015-04-26
  • 2019-11-22
  • 2020-12-09
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
  • 2016-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多