Medium 清新閱讀版
:連結
前一天與大家分享了幾個通用型 Assertion 函數,今天來為大家介紹幾個 HTTP 相關的 Assertion 函數吧!
今天要介紹的各函數,其使用方式和前一天所介紹的略有不同。以下所列各函數,皆是基於 HTTP Response 來做驗證測試,因此大家會看到 $response = $this->get('/')
像這樣的語句,執行到這語句時,PHPUnit 將會執行HTTP Resquest GET /
,相當於用瀏覽器開啟網站根網址,或是用 Postman 打網站的根網址。更多詳細說明可參考此連結。
除此之外,今天會提到的各個 Assertion 函數,並非 PHPUnit 內建,而是由 Laravel 所擴充,因此需注意是否有確實引用到 use Tests\TestCase
,該檔通常位於 tests/
底下:
<?php
// tests/TestCase.php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
}
assertCookie
$response->assertCookie($cookieName, $value = null)
$cookieName
之 Cookie,且是否與給定之 $value
值相等。Route
Route::get('/assertCookie', function () {
return response('')->withCookie('cookieName', 'cookieValue');
});
Test
/**
* Example for assertCookie()
* @return void
*/
public function testAssertCookie()
{
$response = $this->get('/assertCookie');
// 通過測試
$response->assertCookie('cookieName', 'cookieValue');
}
assertCookieMissing
$response->assertCookieMissing($cookieName)
$cookieName
之 Cookie。Route
Route::get('/assertCookieMissing', function () {
return response('')->withCookie('cookieName', 'cookieValue');
});
Test
/**
* Example for assertCookieMissing()
* @return void
*/
public function testAssertCookieMissing()
{
$response = $this->get('/assertCookie');
// 通過測試
$response->assertCookieMissing('cookieName2');
}
assertSessionHas
$response->assertSessionHas($key, $value)
Route
Route::get('/assertSessionHas', function () {
Session::put('sessionKey', 'sessionValue');
return response('');
});
Test
/**
* Example for assertSessionHas()
* @return void
*/
public function testassertSessionHas()
{
$response = $this->get('/assertSessionHas');
// 通過測試
$response->assertSessionHas('sessionKey');
}
assertSuccessful、assertOk、assertNotFound、assertForbidden、assertUnauthorized、assertUnprocessable
$response->assertSuccessful()
$response->assertOk()
$response->assertNotFound()
$response->assertForbidden()
$response->assertUnauthorized()
$response->assertUnprocessable()
assertSuccessful
:回應為成功類HTTP Status Code(>= 200 and < 300)assertOk
:回應為 200 HTTP Status CodeassertNotFound
:回應為 400 HTTP Status CodeassertForbidden
:回應為 403 HTTP Status CodeassertUnauthorized
:回應為 401 HTTP Status CodeassertUnprocessable
:回應為 422 HTTP Status CodeRoute
Route::get('/notFound', function () {
return response('', 404);
});
Route::get('/ok', function () {
return response('', 200);
});
Route::get('/successful', function () {
return response('', 201);
});
Route::get('/forbidden', function () {
return response('', 403);
});
Route::get('/unauthorized', function () {
return response('', 401);
});
Route::get('/unprocessable', function () {
return response('', 422);
});
Test
/**
* Example for assertSuccessful()、assertOk()、assertNotFound()、assertForbidden()、assertUnauthorized()、assertUnprocessable()
* @return void
*/
public function testAssertHttpStatusCode()
{
$response1 = $this->get('/notFound');
$response2 = $this->get('/ok');
$response3 = $this->get('/successful');
$response4 = $this->get('/forbidden');
$response5 = $this->get('/unauthorized');
$response6 = $this->get('/unprocessable');
// 以下各 Assertion 皆會通過測試
$response1->assertNotFound();
$response2->assertOk();
$response3->assertSuccessful();
$response4->assertForbidden();
$response5->assertUnauthorized();
$response6->assertUnprocessable();
}
assertJson
$response->assertJson(array $data, $strict = false)
Route
Route::get('/assertJson', function () {
return response()->json(
[
'field1' => 'value1',
'field2' => 'value2',
]
);
});
Test
/**
* Example for assertJson()
* @return void
*/
public function testAssertJson()
{
$response = $this->get('/assertJson');
// 通過測試
$response->assertJson(['field1' => 'value1']);
}
assertJsonStructure
$response->assertJsonStructure(array $structure)
Route
Route::get('/assertJsonStructure', function () {
return response()->json(
[
'a' => [
'b' => [
'c',
],
],
]
);
});
Test
/**
* Example for assertJsonStructure()
* @return void
*/
public function testAssertJsonStructure()
{
$response = $this->get('/assertJsonStructure');
// 通過測試
$response->assertJsonStructure(['a' => ['b']]);
}
以上就是今天所介紹的 HTTP 相關 Assertion 函數。
明天來介紹資料庫相關的 Assertion 函數。
PS.原本想介紹更多 Assertion 函數,只是 Assertion 函數實在太多,取捨之下,只能先為大家介紹最重要與最常用的幾個