我对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
]);
}
但是在我的控制器中,我使用Auth::id()
获取当前已登录用户的id
。如何在Laravel
5.1中模拟这个呢?
Auth::shouldReceive('id')->andReturn(DB::table('users')->orderBy('id', 'desc')->first(['id'])->id);
- 请记住我没有测试过这个代码,但是应该能够获取最后创建用户的ID。 - samlev$this->be(\App\User::where('username', 'ringo')->first());
看起来它能正常工作。 - user1012181