2017-04-10 112 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