2017-04-10 109 views
0

返回我寫了這個登錄單元測試的laravellaravel單元測試無效的JSON從路線

class LoginTest extends TestCase 
{ 

/** @test */ 
    public function auth() 
    { 
     $this->withoutMiddleware(); 
    $this->visit('/login') 
     ->post('/login', ['[email protected]', "password"=>"1234"]) 
     ->seeJson([ 
      "login"=>"true" 
     ]); 
    $this->get('/dashboard'); 

} 

} 

,但我得到這個錯誤

Invalid JSON was returned from the route. Perhaps an exception was thrown? 

我怎麼能解決這個問題呢?

回答

1

我建議你使用Guzzle Library,使你的請求路徑,然後傾全力應對:

 $response = $http->request('POST', 'http://your_laravel_public/login', [ 
      'form_params'=>[ 
       'username' => '[email protected]', 
       'password' => '1234' 
      ] 
     ]); 
     $response_array = json_decode((string) $response->getBody(), true);//use this to see the actual response 
     dd($response_array); 

,因爲你可能在你的項目以後使用護照或任何其他身份驗證服務,這非常有用。在這一點上,你將需要發送頭授權,你不能通過本地laravel單元測試。

給它一個滾動。

+0

我嘗試第二個,但我得到了這個 錯誤:調用未定義的方法LoginTest :: getBody() – flower

+0

然後請得到在我的答案中提到的槍庫 – Learner