2016-04-07 38 views
2

officiel doc'u izleyerek bir JSON API'sini test etmeye çalışıyorum. Sorunun POST isteğine verilen veri dizisinden geldiğini biliyorum. Test, ['hello' => 'world'] gibi tek seviyeli bir dizi ile iyi çalışıyor, bu yüzden görünüşe göre post fonksiyonu karmaşık yapıları ele alamıyor mu? Burada neyi yanlış yapıyorum?Laravel 5 PHPUnit json gönderimi

Testi:

public function testInsert() 
{ 
    $this->post(
     '/test', 
     [ 
      'content' => 'Hello world!', 
      'count' => [ 
       'a' => 12.345678, 
       'b' => 12.345678 
      ], 
      'user' => [ 
       'id' => 1 
      ] 
     ], 
     ['contentType' => 'application/json'] 
    )->seeJsonEquals([ 
     'status' => true 
    ]); 
} 

Hata:

unknown:myapp nobody$ phpunit 
PHPUnit 4.8.24 by Sebastian Bergmann and contributors. 

E. 

Time: 648 ms, Memory: 15.25Mb 

There was 1 error: 

1) APITest::testInsert 
ErrorException: Invalid argument supplied for foreach() 

/Users/nobody/myapp/vendor/laravel/framework/src/Illuminate/Support/Arr.php:487 
/Users/nobody/myapp/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:231 
/Users/nobody/myapp/tests/APITest.php:43 

FAILURES! 
Tests: 2, Assertions: 2, Errors: 1. 

Denetleyici: Ben laravel new myapp komutunu kullanarak benim laravel uygulamasını yeniden yüklenir ve her şey şimdi gayet iyi çalışıyor

public function store() { 
    $user = User::find(Input::get('user.id')); 

    // TODO: Validate input using JSON schema 

    if (empty($user)) 
     $errors[] = 'User does not exist.'; 

    if (empty(Input::get('content'))) 
     $errors[] = 'Content is empty.'; 

    $a = Input::get('location.latitude'); 
    if ($a < 15) 
     $errors[] = 'A out of range.'; 

    $b = Input::get('location.longitude'); 
    if ($b < 20) 
     $errors[] = 'B out of range.'; 

    if (empty($errors)) { 
     $post = new Post(); 

     $post->content = Input::get('content'); 
     $post->count()->create([ 
      'a' => $a, 
      'b' => $b 
     ]); 

     $user->posts()->save($user); 

     $post->save(); 

     return APIUtils::makeStatusResponse(true); 
    } 

    return APIUtils::makeStatusResponse(false, $errors); 
} 
+0

Denetleyici yönteminizi de gönderin. –

cevap

0

. Sorunun, composer create-project --prefer-dist laravel/laravel may ile yapılan önceki yüklemeden geldiğini varsayıyorum.