php - 单元测试 Laravel 5.1 中的用户身份验证

标签 php unit-testing laravel-5.1

我对 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/

相关文章:

php - 从每个类别的多个表中选择最新记录

php - 在 PHP 中看不到 Python 模块

php - 使用 PHP 或 Javascript 自动将浏览器重定向到多个 IP 地址

php - Laravel:查询生成器连接多表

php - 我认为 MySQL 查询错误

c# - 如何对该异步代码进行单元测试?

安卓工作室 : Can not find Run test choice

c# - 尽管定义了返回对象,NSubstitute Async 仍返回 null

php - Memcached - 升级到 ubuntu 16.04 后出现 "No Memcached servers added"

mongodb - Yajra laravel 数据表包在带有 mongodb 的 laravel 5.1 中无法正常工作