久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      介紹Laravel unit test : 模擬認(rèn)證的用戶

      Laravel unit test : 模擬認(rèn)證的用戶

      在 Laravel 編寫單元測試時(shí)經(jīng)常會遇到需要模擬認(rèn)證用戶的時(shí)候,比如新建文章、創(chuàng)建訂單等,那么在 Laravel unit test 中如何來實(shí)現(xiàn)呢?

      官方解決方法

      Laravel 的官方文檔中的測試章節(jié)中有提到:

      Of course, one common use of the session is for maintaining state for the authenticated user. The actingAs helper method provides a simple way to authenticate a given user as the current user. For example, we may use a model factory to generate and authenticate a user:

      <?php  use AppUser;  class ExampleTest extends TestCase {     public function testApplication()     {         $user = factory(User::class)->create();          $response = $this->actingAs($user)                          ->withSession(['foo' => 'bar'])                          ->get('/');     } }

      其實(shí)就是使用 Laravel Testing IlluminateFoundationTestingConcernsImpersonatesUsers Trait 中的 actingAsbe 方法。

      設(shè)置以后在后續(xù)的測試代碼中,我們可以通過 auth()->user() 等方法來獲取當(dāng)前認(rèn)證的用戶。

      偽造認(rèn)證用戶

      在官方的示例中有利用 factory 來創(chuàng)建一個(gè)真實(shí)的用戶,但是

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號