2017-02-02 11 views
0

私は自分のプロジェクトでPOSTメソッドをテストしており、多次元配列をパラメータとして呼び出します。これをLaravelのpost()メソッドに渡すにはどうすればよいですか?私はまたpostJson()メソッドを試しています。LaravelでPOST呼び出しをテストする際に多次元配列を渡す方法は?

public function testExcludeFilter() 
    { 
     $json = ' 
     { 
      "northLatitude":45.123456, 
      "southLatitude":45.123456, 
      "eastLongitude":9.1234567, 
      "westLongitude":9.1234567, 
      "filters": { 
       "excludes": [ 
        1 
       ] 
      } 
     } 
     '; 

     $response = $this->postJson('api/v1/getHotels', $json) 
      ->dontSeeJson([ 
       'id' => 1, 
       'name' => 'Hotel-1', 
       'latitude' => 45.123456, 
       'longitude' => 9.1234567 
       ]); 

     $response->assertResponseStatus(200); 
    } 

エラーを受信:私がテストしてるマルチアレイは、フィルタ[「除外」]である私も配列として私のデータを渡そうとしました

ErrorException: Argument 2 passed to Illuminate\Foundation\Testing\TestCase::postJson() must be of the type array, string given

が、それはしませんでした除外フィルターに注意を払う:

postJson('api/v1/getHotels', ['northLatitude' => '45.123456', 'southLatitude' => '45.123456', 'westLongitude' => '9.1234567', 'eastLongitude' => '9.1234567', 'filters["excludes"]' => [1]] 
+1

'$ response = $ this-> postJson( 'api/v1/getHotels'、json_decode($ json、true))'これはjsonを配列に変換します –

+0

ありがとう、これは答えです – f7n

+0

@MASIDDIQUI答えにコメントを付けて、質問に回答としてマークすることができます –

答えて

0
$response = $this->postJson('api/v1/getHotels', json_decode($json, true)) 
      ->dontSeeJson([ 
       'id' => 1, 
       'name' => 'Hotel-1', 
       'latitude' => 45.123456, 
       'longitude' => 9.1234567 
       ]); 

Json_decode($のJSON、真)は、あなたのJSONダを変換します配列へ

関連する問題