2016-07-26 2 views
-1

나는 doctrine을 실행하는 컨트롤러에서 기능 테스트를 시도합니다. 테스트를 실행할 때 실패합니다. 하지만 내 컨트롤러에서이 줄에 주석을 달았을 때 : "$ products = $ em-> getRepository ("Couture \ FrontBundle \ Entity \ Produit ") -> findAll()". 시험은 성공적입니다.PB 기능 테스트 symfony2 컨트롤러

class ProductControllerTest extends WebTestCase { 

    public function testGetAll() { 

    $client = static::createClient(); 

    $client->request('GET', $client->getContainer()->get('router')->generate('couture_front_product_getall')); 

    $this->assertEquals(
      200, $client->getResponse()->getStatusCode() 
    ); 
    } 
} 

답변

0

해당 라인을 주석이 !$productsfalse하고 다음 컨트롤러가 Reponse 개체를 반환 :이 내 수업 테스트

class ProductController extends Controller { 
    /** 
    * Get products 
    * @Route("/products") 
    * @Method("GET") 
    */ 
    public function getAllAction() { 

    $serialize = $this->get('jms_serializer'); 

    $em = $this->getDoctrine(); 

    $products = $em->getRepository('Couture\FrontBundle\Entity\Produit')->findAll(); 

    if (!$products) { 

     $response = new Response(json_encode(array('error' => 'Resources not found for products'))); 
     $response->headers->set('Content-Type', 'application/json'); 
     $response->setStatusCode('400'); 
     return $response; 
    } 

    $response = new Response($serialize->serialize($products, 'json')); 

    $response->headers->set('Content-Type', 'application/json'); 

    return $response; 
    } 
} 

입니다 :

내 컨트롤러 application/json 콘텐츠 유형 헤더 이 응답은 상태 코드가 200 OK이므로 성공한 것으로 간주됩니다.

필자가 본 시나리오는 doctrine entityManager가 관리하는 엔티티를 테스트하기 위해 데이터 픽스처를 사용하는 것입니다.

테스트 클래스 (바로 뒤에 당신에게 아이디어를주고, 테스트되지 않은 코드)과 같이 보일 수 있습니다 : 내가 개입 확인하는 전문가 너무 저를 기다리고

class ProductControllerTest extends WebTestCase { 

public function testGetAll() { 

     $client = static::createClient(); 

     $fixtures = array('Acme\YourBundle\dataFixtures\LoadProductData'); 

     $this->loadFixtures($fixtures); 

     $uri= $this->getUrl('couture_front_product_getall'); 

     $crawler= $client->request('GET',$uri); 


     $this->assertEquals(200,$client->getResponse()->getStatusCode()); 

} 
} 

. 방금 알게 된 내용을 공유하고 커뮤니티에서 배울 수있는 짧은 의견을 발견했습니다. 희망 도움이