<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                * * * * * [TOC] ## 簡介 Laravel 為 HTTP 請求的生成和輸出的檢查都提供了非常流暢的 API。例如,你可以查看下面的這個測試用例: ~~~ <?php namespace Tests\Feature; use Tests\TestCase; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithoutMiddleware; class ExampleTest extends TestCase { /** * 一個基礎的測試用例。 * * @return void */ public function testBasicTest() { $response = $this->get('/'); $response->assertStatus(200); } } ~~~ `get`?方法會創建一個?`GET`?請求來請求你的應用,而?`assertStatus`?方法斷言返回的響應是指定的 HTTP 狀態碼。除了這個簡單的斷言之外,Laravel 也包含檢查響應標頭、內容、JSON 結構等各種斷言。 ### 自定義請求頭 您可以使用?`withHeaders`?方法在發送到應用程序之前自定義請求的標頭。 你可以添加想要的任何自定義標題: ~~~ <?php class ExampleTest extends TestCase { /** * 一個基本的功能測試示例。 * * @return void */ public function testBasicExample() { $response = $this->withHeaders([ 'X-Header' => 'Value', ])->json('POST', '/user', ['name' => 'Sally']); $response ->assertStatus(200) ->assertJson([ 'created' => true, ]); } } ~~~ > {tip} 運行測試時,CSRF 中間件會自動禁用。 ## Session / 認證 Laravel 提供了幾個可在測試時使用 Session 的輔助函數。首先,你需要傳遞一個數組給?`withSession`?方法來設置 Seesion 數據。這讓你在應用程序的測試請求發送之前,先給數據加載 Session 變得簡單: ~~~ <?php class ExampleTest extends TestCase { public function testApplication() { $response = $this->withSession(['foo' => 'bar']) ->get('/'); } } ~~~ 當然,一般使用 Session 時都是用于維持用戶的狀態,如認證用戶。`actingAs`?輔助函數提供了簡單的方式來讓指定的用戶認證為當前的用戶。例如,我們可以使用?[工廠模型](http://www.hmoore.net/tonyyu/laravel_5_6/786284#_68)?來生成并認證用戶: ~~~ <?php use App\User; class ExampleTest extends TestCase { public function testApplication() { $user = factory(User::class)->create(); $response = $this->actingAs($user) ->withSession(['foo' => 'bar']) ->get('/'); } } ~~~ 你也可以通過傳遞 guard 名稱作為?`actingAs`?的第二參數以指定用戶通過哪種 guard 來認證: ~~~ $this->actingAs($user, 'api') ~~~ ## 測試 JSON API Laravel 也提供了幾個輔助函數來測試JSON API 和它們的相應。例如?`json`,?`get`,?`post`,?`put`,?`patch`?和`delete`?可以被用于發送各種 HTTP 動作。你也可以輕松地將數據和請求頭傳遞到這些方法中。讓我們寫一個?`POST`請求到?`/user`?并斷言返回期望的數據來開始使用它們: ~~~ <?php class ExampleTest extends TestCase { /** * 一個基礎的功能測試示例。 * * @return void */ public function testBasicExample() { $response = $this->json('POST', '/user', ['name' => 'Sally']); $response ->assertStatus(200) ->assertJson([ 'created' => true, ]); } } ~~~ > {tip}?`assertJson`?方法將響應轉換為數組并且利用?`PHPUnit::assertArraySubset`?來驗證給定的數組存在于應用返回的 JSON 響應中。所以,如果 JSON 相應中如果有其他屬性,測試仍舊會在給定數組存在的情況下通過。 ### 驗證完全匹配 如果你想驗證應用返回的 JSON?**完全**?匹配給定的數組,你應該使用?`assertExactJson`?方法: ~~~ <?php class ExampleTest extends TestCase { /** * 一個基礎的功能測試示例。 * * @return void */ public function testBasicExample() { $response = $this->json('POST', '/user', ['name' => 'Sally']); $response ->assertStatus(200) ->assertExactJson([ 'created' => true, ]); } } ~~~ ## 測試文件上傳 進行測試時,這個?`Illuminate\Http\UploadedFile`?類提供了一個可能用來生成虛擬的文件或者圖片的?`fake`?方法。它與這個?`Storage`?Facade 的?`fake`?方法結合在一起巨大的簡化了文件上傳的測試。 例如 , 你可能把那兩個特征結合起來很容易測試一個頭像的上傳成功: ~~~ <?php namespace Tests\Feature; use Tests\TestCase; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithoutMiddleware; class ExampleTest extends TestCase { public function testAvatarUpload() { Storage::fake('avatars'); $response = $this->json('POST', '/avatar', [ 'avatar' => UploadedFile::fake()->image('avatar.jpg') ]); // 驗證文件已存儲 ... Storage::disk('avatars')->assertExists('avatar.jpg'); // 驗證一個文件不存在 ... Storage::disk('avatars')->assertMissing('missing.jpg'); } } ~~~ #### 虛擬文件定義 當使用這個?`fake`?方法創建文件時,為了更好地測試你想要的圖片尺寸,你也許會設定圖片的寬度,高度,以及圖片的大小 : ~~~ UploadedFile::fake()->image('avatar.jpg', $width, $height)->size(100); ~~~ 此外,在進行創建圖片時,你可能使用這個?`create`?方法創建其它任何類型的文件: ~~~ UploadedFile::fake()->create('document.pdf', $sizeInKilobytes); ~~~ ## 可用斷言 ### 響應斷言 `Laravel`?為您的?[PHPUnit](https://phpunit.de/)?測試提供了各種常規斷言方法。 這些斷言可以通過從?`json`,?`get`,?`post`,?`put`, 和?`delete`?測試方法返回的響應來訪問: [assertCookie](#_Cookie_249) [assertCookieExpired](#_Cookie__257) [assertCookieMissing](#_Cookie__265) [assertDontSee](#assertDontSee_273) [assertDontSeeText](#assertDontSeeText_281) [assertExactJson](#assertExactJson_289) [assertHeader](#assertHeader_297) [assertHeaderMissing](#assertHeaderMissing_305) [assertJson](#assertJson_313) [assertJsonFragment](#assertJsonFragment_321) [assertJsonMissing](#assertJsonMissing_329) [assertJsonMissingExact](#assertJsonMissingExact_337) [assertJsonStructure](#assertJsonStructure_345) [assertJsonValidationErrors](#assertJsonValidationErrors_353) [assertPlainCookie](#assertPlainCookie_361) [assertRedirect](#assertRedirect_369) [assertSee](#assertSee_377) [assertSeeInOrder](#assertSeeInOrder_385) [assertSeeText](#assertSeeText_393) [assertSeeTextInOrder](#assertSeeTextInOrder_401) [assertSessionHas](#assertSessionHas_409) [assertSessionHasAll](#assertSessionHasAll_417) [assertSessionHasErrors](#assertSessionHasErrors_425) [assertSessionHasErrorsIn](#assertSessionHasErrorsIn_433) [assertSessionMissing](#assertSessionMissing_441) [assertStatus](#assertStatus_449) [assertSuccessful](#assertSuccessful_457) [assertViewHas](#assertViewHas_465) [assertViewHasAll](#assertViewHasAll_473) [assertViewIs](#assertViewIs_481) [assertViewMissing](#assertViewMissing_489) #### 斷言 Cookie 斷言此響應包含給定的?`cookie`: ~~~ $response->assertCookie($cookieName, $value = null); ~~~ #### 斷言 Cookie 過期 斷言此響應包含給定的?`cookie`?并且其已過期: ~~~ $response->assertCookieExpired($cookieName); ~~~ #### 斷言 Cookie 丟失 斷言此響應不包含給定的?`cookie`: ~~~ $response->assertCookieMissing($cookieName); ~~~ #### assertDontSee 驗證所給的字符串不包含在響應中: ~~~ $response->assertDontSee($value); ~~~ #### assertDontSeeText 驗證所給的字符串不包含在響應的文本中: ~~~ $response->assertDontSeeText($value); ~~~ #### assertExactJson 驗證響應和所給 JSON 數據完全符合: ~~~ $response->assertExactJson(array $data); ~~~ #### assertHeader 驗證所給的頭目前在響應中: ~~~ $response->assertHeader($headerName, $value = null); ~~~ #### assertHeaderMissing 驗證所給的頭目前沒有在響應中: ~~~ $response->assertHeaderMissing($headerName); ~~~ #### assertJson 驗證響應包含所給的 JSON 數據: ~~~ $response->assertJson(array $data); ~~~ #### assertJsonFragment 驗證此響應包含給定的 JSON 片段: ~~~ $response->assertJsonFragment(array $data); ~~~ #### assertJsonMissing 驗證此響應不包含給定的 JSON 片段: ~~~ $response->assertJsonMissing(array $data); ~~~ #### assertJsonMissingExact 驗證此響應不包含確切的 JSON 片段 ~~~ $response->assertJsonMissingExact(array $data); ~~~ #### assertJsonStructure 驗證此響應含有給定的 JSON 結構: ~~~ $response->assertJsonStructure(array $structure); ~~~ #### assertJsonValidationErrors 驗證此響應有給定鍵的 JSON 驗證錯誤 : ~~~ $response->assertJsonValidationErrors($keys); ~~~ #### assertPlainCookie 驗證此響應包含所給的 cookie 『 加密 』: ~~~ $response->assertPlainCookie($cookieName, $value = null); ~~~ #### assertRedirect 斷言響應重定向到指定 URI: ~~~ $response->assertRedirect($uri); ~~~ #### assertSee 斷言響應中包含指定字符串: ~~~ $response->assertSee($value); ~~~ #### assertSeeInOrder 斷言響應中有序包含指定字符串: ~~~ $response->assertSeeInOrder(array $values); ~~~ #### assertSeeText 斷言響應文本中包含指定字符串: ~~~ $response->assertSeeText($value); ~~~ #### assertSeeTextInOrder 斷言響應文本中有序包含指定字符串: ~~~ $response->assertSeeTextInOrder(array $values); ~~~ #### assertSessionHas 斷言 session 包含數據片段: ~~~ $response->assertSessionHas($key, $value = null); ~~~ #### assertSessionHasAll 斷言 session 中存在指定的所有值: ~~~ $response->assertSessionHasAll($key, $value = null); ~~~ #### assertSessionHasErrors 斷言 session 中含有指定錯誤: ~~~ $response->assertSessionHasErrors(array $keys, $format = null, $errorBag = 'default'); ~~~ #### assertSessionHasErrorsIn 斷言 session 中含有指定錯誤: ~~~ $response->assertSessionHasErrorsIn($errorBag, $keys = [], $format = null); ~~~ #### assertSessionMissing 斷言 session 中不含有指定鍵: ~~~ $response->assertSessionMissing($key); ~~~ #### assertStatus 斷言響應中存在指定狀態碼: ~~~ $response->assertStatus($code); ~~~ #### assertSuccessful 斷言響應中存在成功狀態碼: ~~~ $response->assertSuccessful(); ~~~ #### assertViewHas 斷言響應視圖中存在指定數據片段: ~~~ $response->assertViewHas($key, $value = null); ~~~ #### assertViewHasAll 斷言響應視圖中存在指定的所有數據: ~~~ $response->assertViewHasAll(array $data); ~~~ #### assertViewIs 斷言響應視圖與指定值一致: ~~~ $response->assertViewIs($value); ~~~ #### assertViewMissing 斷言響應視圖缺少一個綁定的數據: ~~~ $response->assertViewMissing($key); ~~~ ### 認證斷言 `Laravel`?為您的?[PHPUnit](https://phpunit.de/)?測試提供了多種身份認證相關的斷言: | 方法 | 描述 | | --- | --- | | `$this->assertAuthenticated($guard = null);` | 斷言此用戶已被認證 | | `$this->assertGuest($guard = null);` | 斷言此用戶未被認證 | | `$this->assertAuthenticatedAs($user, $guard = null);` | 斷言給定的用戶被認證 | | `$this->assertCredentials(array $credentials, $guard = null);` | 斷言給定的憑證有效 | | `$this->assertInvalidCredentials(array $credentials, $guard = null);` | 斷言給定的憑證無效 | 本文章首發在?
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看