2014-02-05 2 views
5

단위 테스트를 실행할 때 아래 오류가 발생합니다. 그것은 Input :: get을 생성자에 전달하는 것을 좋아하지 않는 것 같지만, 브라우저 내에서 스크립트를 실행할 때 동작이 제대로 작동하므로 컨트롤러 코드가 아님을 알 수 있습니다. 'task_update'코드 중 하나를 꺼내면 입력이있을 때만 테스트가 통과하므로 하나의 메소드에 대한 입력을 받아들이는 이유는 확실하지 않습니다. 포착되지 않는 그냥 내가 task_updates 배열의 입력을하고 있어요하지만 -Laravel - 단위 테스트를 통과하지 못하는 입력

public function store() 
{ 
    $task_update = new TaskUpdate(Input::get('tasks_updates')); 

    $task = $this->task->find(Input::get('tasks_updates')['task_id']); 

    $output = $task->taskUpdate()->save($task_update); 

    if (!!$output->id) { 
     return Redirect::route('tasks.show', $output->task_id) 
         ->with('flash_task_update', 'Task has been updated'); 
    } 
} 

그리고 테스트는 다음과 같습니다 :

Input::replace(['tasks_updates' => array('description' => 'Hello')]); 

    $mockClass = $this->mock; 
    $mockClass->task_id = 1; 

    $this->mock->shouldReceive('save') 
       ->once() 
       ->andReturn($mockClass); 

    $response = $this->call('POST', 'tasksUpdates'); 

    $this->assertRedirectedToRoute('tasks.show', 1); 
    $this->assertSessionHas('flash_task_update'); 

답변

4

내가 믿는

ErrorException: Argument 1 passed to Illuminate\Database\Eloquent\Model::__construct() must be of the type array, null given, called 

내 컨트롤러입니다 "call"함수는 Input :: replace에 의해 수행 된 작업을 날려 버리고있다.

호출 함수는 실제로 문제를 해결하는 $ parameters 매개 변수를 사용할 수 있습니다.

당신이 전화 @ \를 분명히 \ 재단 \ 테스트 \의 TestCase에서 보면, 당신은 함수를 볼 수 있습니다 :

/** 
* Call the given URI and return the Response. 
* 
* @param string $method 
* @param string $uri 
* @param array $parameters 
* @param array $files 
* @param array $server 
* @param string $content 
* @param bool $changeHistory 
* @return \Illuminate\Http\Response 
*/ 
public function call() 
{ 
    call_user_func_array(array($this->client, 'request'), func_get_args()); 

    return $this->client->getResponse(); 
} 

당신이 할 경우

$response = $this->call('POST', 'tasksUpdates', array('your data here')); 

내가이 일을해야한다고 생각합니다.

1

나는 Input::replace($input)$this->call('POST', 'path', $input) 모두를 선호합니다.

예 AuthControllerTest.php :

public function testStoreSuccess() 
{ 
    $input = array(
     'email' => '[email protected]', 
     'password' => 'password', 
     'remember' => true 
     ); 

    // Input::replace($input) can be used for testing any method which  
    // directly gets the parameters from Input class 
    Input::replace($input); 



    // Here the Auth::attempt gets the parameters from Input class 
    Auth::shouldReceive('attempt') 
    ->with(  
      array(
        'email' => Input::get('email'), 
        'password' => Input::get('password') 
      ), 
      Input::get('remember')) 
    ->once() 
    ->andReturn(true); 

    // guarantee we have passed $input data via call this route 
    $response = $this->call('POST', 'api/v1/login', $input); 
    $content = $response->getContent(); 
    $data = json_decode($response->getContent()); 

    //... assertions 

}