5

単体テストを実行すると、次のエラーが表示されます。Input::get をコンストラクターに渡すのは好きではないようですが、ブラウザー内でスクリプトを実行すると、アクションは正常に機能するため、コントローラー コードではないことがわかります。「task_update」コードのいずれかを取り出すと、入力があっても検索だけでテストに合格します。そのため、1 つのメソッドの入力を受け入れる理由がわかりません。

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

私のコントローラーは:

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');
    }
}

そしてテストは - task_updates 配列の入力を設定していますが、ピックアップされていません:

    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

2 に答える 2

4

「call」関数は、Input::replace によって行われた作業を吹き飛ばしていると思います。

call 関数は実際に問題を解決する $parameters パラメータを取ることができます。

\Illuminate\Foundation\Testing\TestCase@call を見ると、次の関数が表示されます。

/**
 * 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'));

うまくいくはずだと思います。

于 2014-02-28T19:54:17.347 に答える
1

Input::replace($input)私は と の両方を行うことを好みます$this->call('POST', 'path', $input)

AuthControllerTest.php の例:

public function testStoreSuccess()
{
    $input = array(
        'email' => 'email@gmail.com', 
        '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

}
于 2014-06-12T08:50:35.833 に答える