我对 Laravel 5.1 中的单元测试有点困惑。
我想测试来自用户帐户的设置更新。我正在使用 phpunit
进行测试。
这是我的测试用例。
/** @test */
public function it_updates_personal_details(){
$this->withoutMiddleware();
$this->visit('/account/settings')
->post('/account/settings', ["firstname"=>"RingoUpdated", 'lastname'=>"RingyUpdated", "username"=>"ringo", "bio"=>"My Personal Information", "facebook"=>"myFbUsername", "twitter"=>"myTwUsername", ])
->seeJson([
"signup"=>true
]);
}
但在我的 Controller 中,我使用 Auth::id()
来获取当前登录用户的 id
。如何在 Laravel
5.1 中模拟这个?
最佳答案
最简单的方法是使用Mockery in the Facade :
public function it_updates_personal_details(){
Auth::shouldReceive('id')->andReturn(1);
$this->withoutMiddleware();
$this->visit('/account/settings')
->post('/account/settings', ["firstname"=>"RingoUpdated", 'lastname'=>"RingyUpdated", "username"=>"ringo", "bio"=>"My Personal Information", "facebook"=>"myFbUsername", "twitter"=>"myTwUsername", ])
->seeJson([
"signup"=>true
]);
}
关于php - 单元测试 Laravel 5.1 中的用户身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33532610/