<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                * * * * * [TOC] ## 基礎介紹 Laravel 為 HTTP 請求的生成和發送操作、輸出的檢查都提供了非常流利的 API。例如,你可以看看下面的這個測試用例: ~~~ <?php namespace Tests\Feature; use Tests\TestCase; use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; class ExampleTest extends TestCase { /** * A basic test example. * * @return void */ public function testBasicTest() { $response = $this->get('/'); $response->assertStatus(200); } } ~~~ `get`?方法會創建一個?`GET`?請求來請求你的應用,而?`assertStatus`?方法斷言返回的響應是給定的 HTTP 狀態碼。除了這個簡單的斷言之外,Laravel 也包含檢查響應標頭、內容、 JSON 結構等等的各種斷言。 ## Session / 認證 Laravel 提供了幾個可在測試時使用 Session 的輔助函數。首先,你需要傳遞一個數組給?`withSession`?方法來設置 Seesion 數據。這讓你在應用程序的測試請求發送之前,先給數據加載 Session 變得簡單: ~~~ <?php class ExampleTest extends TestCase { public function testApplication() { $response = $this->withSession(['foo' => 'bar']) ->get('/'); } } ~~~ 當然,一般使用 Session 時都是用于維持用戶的狀態,如認證用戶。`actingAs`?輔助函數提供了簡單的方式來讓指定的用戶認證為當前的用戶。例如,我們可以使用?[模型工廠](https://laravel-china.org/docs/laravel/5.4/database-testing#writing-factories)?來生成并認證用戶: ~~~ <?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 APIs Laravel 也提供了幾個輔助函數來測試 JSON APIs 及其響應。舉例來說,`json`,?`get`,?`post`,?`put`,?`patch`?和?`delete`?方法可以用于發出各種 HTTP 動作的請求。你也可以輕松的傳入數據或標頭到這些方法上。首先,讓我們來編寫一個測試,將 POST 請求發送至 /user ,并斷言其會返回預期數據: ~~~ <?php class ExampleTest extends TestCase { /** * A basic functional test example. * * @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 如果你想驗證傳入的數組是否與應用程序返回的 JSON?**完全**?匹配,你可以使用?`assertExactJson`?方法: ~~~ <?php class ExampleTest extends TestCase { /** * A basic functional test example. * * @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\WithoutMiddleware; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; 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`這些測試方法返回的響應都可以使用這些斷言方法: | Method | Description | | --- | --- | | `$response->assertStatus($code);` | 斷言該響應具有給定的狀態碼。 | | `$response->assertRedirect($uri);` | 斷言該響應被重定向至指定的 URI。 | | `$response->assertHeader($headerName, $value = null);` | 斷言該響應存在指定的標頭。 | | `$response->assertCookie($cookieName, $value = null);` | 斷言該響應包含了指定的 cookie。 | | `$response->assertPlainCookie($cookieName, $value = null);` | 斷言該響應包含了指定的 cookie (未加密)。 | | `$response->assertSessionHas($key, $value = null);` | 斷言該 session 包含指定數據。 | | `$response->assertSessionHasErrors(array $keys);` | 斷言該 session 包含指定字段的錯誤信息。 | | `$response->assertSessionMissing($key);` | 斷言該 session 不包含指定鍵。 | | `$response->assertJson(array $data);` | 斷言該響應包含指定 JSON 數據。 | | `$response->assertJsonFragment(array $data);` | 斷言該響應包含指定 JSON 片段。 | | `$response->assertExactJson(array $data);` | 斷言該響應包含完全匹配的 JSON 數據。 | | `$response->assertJsonStructure(array $structure);` | 斷言該響應存在指定 JSON 結構。 | | `$response->assertViewHas($key, $value = 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>

                              哎呀哎呀视频在线观看