【问题标题】:User Authentication in Unit Testing Laravel 5.1单元测试 Laravel 5.1 中的用户身份验证
【发布时间】:2015-11-04 21:44:30
【问题描述】:

我对 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 中模拟这个?

【问题讨论】:

    标签: php unit-testing laravel-5.1


    【解决方案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
            ]);
    }
    

    【讨论】:

    • 运行测试时,我正在创建一个测试用户。有什么方法可以获取该用户的id
    • 你可以做这样的事情(它很丑,但可能):Auth::shouldReceive('id')->andReturn(DB::table('users')->orderBy('id', 'desc')->first(['id'])->id); - 请记住,我没有测试过这个,但应该得到最后创建的用户的 ID。
    • 其实……这可能行不通。它会在您添加的新用户之前获得最后一个用户。
    • 还有其他方法可以做到这一点吗?
    • 如果不了解您的架构或您正在调用的功能,很难说。您可以使用该方法,但将结果加 1,并假装这是正确的(甚至更不理想)。这实际上取决于您实际创建用户的方式等。
    【解决方案2】:

    太棒了,我们可以为所有测试用例转移到 TestCase 类,我们可以给一个名字,像这样:

    public function loginWithFakeUser()
    {
          $user = new User([
            'id' => 1,
            'name' => 'yish'
          ]);
    
          $this->be($user);
    }
    

    当我们需要认证用户时,我们可以调用 loginWithFakeUser 来伪装用户。

    【讨论】:

    • 使用这个,我在这一行得到一个空的Call to a member function guard()$this->app['auth']->guard($driver)->setUser($user);。这是在 Laravel 5.8 中
    【解决方案3】:

    actingAs 辅助方法。 Laravel 5.3

    <?php
    
    use Illuminate\Foundation\Testing\WithoutMiddleware;
    use Illuminate\Foundation\Testing\DatabaseMigrations;
    use Illuminate\Foundation\Testing\DatabaseTransactions;
    
    use App\User; // user model
    
    class ExampleTest extends TestCase
    {
        public function testApplication()
        {
            $user = User::find(1); // find specific user
    
            $this->actingAs($user)
                 ->visit('/')
                 ->see('Hello, '.$user->name);
        }
    }
    

    actingAs 辅助方法。提供了一种将给定用户验证为当前用户的简单方法。

    您还可以使用模型工厂来生成和验证用户:

            $user = factory(App\User::class)->create();
            $this->actingAs($user)
             ->visit('/')
             ->see('Hello, '.$user->name);
    

    【讨论】:

      猜你喜欢
      • 2015-10-21
      • 1970-01-01
      • 2018-02-14
      • 2016-06-11
      • 1970-01-01
      • 2014-06-24
      • 2021-11-23
      • 2015-09-23
      • 1970-01-01
      相关资源
      最近更新 更多