2012-07-26 5 views
1

json 페이로드를 허용하는 컨트롤러 함수를 테스트하려고합니다.Cakephp testAction()이 컨트롤러에 json 페이로드를 보내지 않습니다.

testAction()의 문서에 따라 $ options [ 'data']를 적절한 문자열로 설정하면됩니다. 그게 나를 위해 작동하지 않습니다. 여기에 인용 된 문서를 참조하십시오 : http://api20.cakephp.org/class/controller-test-case (testAction() 섹션을 아래로 스크롤하십시오).

내 테스트 사례입니다.

public function testCreate(){ 
    //Some code here 
    $this->testAction('/shippingbatches/create', array('data' => '[3,6]', 'method' => 'post')); 
    //Some more code here 
} 

여기 내 컨트롤러 기능

public function create(){   
    debug($this->request); //This debug shows me an empty array in $this->request->data 
    ob_flush(); 
    $order_ids = json_decode($this->request->data); 
    //Some more code here 
} 

컨트롤러 함수의 첫 번째 라인은 저 $ this-> 요청 -> 데이터 빈 어레이를 보이고있다. testAction()에서 전달 된 '데이터'가 실제 배열이면 nice &이됩니다. 하지만 그것은 문자열로 설정되어 있지 않습니다 (설명서에 나와있는 것과는 다릅니다).

다음은 디버그 결과입니다.

object(Mock_CakeRequest_ef4431a5) { 
    params => array(
     'plugin' => null, 
     'controller' => 'shippingbatches', 
     'action' => 'create', 
     'named' => array(), 
     'pass' => array(), 
     'return' => (int) 1, 
     'bare' => (int) 1, 
     'requested' => (int) 1 
    ) 
    data => array() 
    query => array(
     'case' => 'Controller\ShippingBatchesController' 
    ) 
    url => 'shippingbatches/create' 
    base => '' 
    webroot => '/' 
    here => '/shippingbatches/create' 
} 

도와주세요.

Gurpreet는

답변

0

같은 데이터를 전달, 당신은 CakeRequest::input()를 사용하여 받아야합니다.

public function create() { 
    $json = $this->request->input('json_decode', true); 
    debug($json); 
} 

내가 ControllerTestCase::testAction을위한 케이크의 테스트 케이스를 읽고이 발견 있음을 알아 두셔야합니다. 테스트 사례를 읽으면 Cake의 내부 구조가 어떻게 작동하는지 파악하고 테스트 작성에 대한 힌트를 얻을 수 있습니다.

+0

감사합니다. 그렇지만 어떻게하면 아이디어를 테스트를 읽는 지 알 수있을 것입니다 ... –

+0

그 테스트는 테스트 어플리케이션의'TestsAppsPostsController :: input_data()'를 사용합니다. 거기를보고, 나는'CakeRequest :: input()'이 입력을 잡는 데 사용된다는 것을 발견했다. – jeremyharris

관련 문제