LoginSignup
26
11

More than 5 years have passed since last update.

Laravel HTTPテスト POSTで値を投げる方法

Last updated at Posted at 2018-08-10

前提

Laravel 5.5
PHP 7.1
PHPUnit 6.5.8

書くこと

LaravelのHTTPテストでPOST値を投げる書き方
(公式ドキュメントで見つけられなかった。 見逃したかな)

やり方

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{

    public function test_商品の登録処理()
    {

         //第二引数にPOST値の配列を渡すだけ
         $response = $this->post('/item/add',[
            'item_id' => '1',
            'item_name' => 'ほげほげ'
        ]);

        //登録処理が完了して、一覧画面にリダイレクトすることを検証
        $response->assertRedirect('/list');
    }

    //POSTするフォームが配列型の場合(items[][item_id]みたいな
    public function test_複数商品の登録処理()
    {

         //第二引数にPOST値の配列を渡すだけ
         $response = $this->post('/item/add',[
            'items' => [
                 [
                     'item_id' => '1',
                     'item_name' => 'ほげほげ'
                 ],
                 [
                     'item_id' => '2',
                     'item_name' => 'ほげほげ2'
                 ],
             ];
        ]);

        //登録処理が完了して、一覧画面にリダイレクトすることを検証
        $response->assertRedirect('/list');
    }
}

26
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
11