W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
功能測(cè)試使您可以查看對(duì)應(yīng)用程序的一次調(diào)用的結(jié)果。這可能是返回單個(gè)Web表單的結(jié)果,命中API端點(diǎn)等等。這很方便,因?yàn)樗试S您測(cè)試單個(gè)請(qǐng)求的整個(gè)生命周期,確保路由有效,響應(yīng)是正確的格式,分析結(jié)果等等。
功能測(cè)試要求您所有的測(cè)試類都對(duì)該CodeIgniter\Test\FeatureTestCase
類進(jìn)行擴(kuò)展。由于這擴(kuò)展了CIDatabaseTestCase,因此您必須始終確保 在執(zhí)行操作之前調(diào)用parent::setUp()
和parent::tearDown()
。
<?php namespace App;
use CodeIgniter\Test\FeatureTestCase;
class TestFoo extends FeatureTestCase
{
public function setUp()
{
parent::setUp();
}
public function tearDown()
{
parent::tearDown();
}
}
本質(zhì)上,F(xiàn)eatureTestCase僅允許您在應(yīng)用程序上調(diào)用終結(jié)點(diǎn)并返回結(jié)果。為此,您可以使用call()
方法。第一個(gè)參數(shù)是要使用的HTTP方法(最常見(jiàn)的是GET或POST)。第二個(gè)參數(shù)是您網(wǎng)站上要測(cè)試的路徑。第三個(gè)參數(shù)接受一個(gè)數(shù)組,該數(shù)組用于填充您正在使用的HTTP動(dòng)詞的超全局變量。因此,GET方法將填充$ _GET變量,而發(fā)布請(qǐng)求將填充$ _POST數(shù)組。
// Get a simple page
$result = $this->call('get', site_url());
// Submit a form
$result = $this->call('post', site_url('contact'), [
'name' => 'Fred Flintstone',
'email' => 'flintyfred@example.com'
]);
存在用于每個(gè)HTTP動(dòng)詞的簡(jiǎn)寫方法,以簡(jiǎn)化鍵入并使內(nèi)容更清晰:
$this->get($path, $params);
$this->post($path, $params);
$this->put($path, $params);
$this->patch($path, $params);
$this->delete($path, $params);
$this->options($path, $params);
注解
$ params數(shù)組并不是每個(gè)HTTP動(dòng)詞都有意義,但為了保持一致性而包含了該數(shù)組。
您可以通過(guò)將“路線”數(shù)組傳遞給withRoutes()
方法來(lái)使用路線的自定義集合。這將覆蓋系統(tǒng)中的所有現(xiàn)有路由:
$routes = [
[ 'get', 'users', 'UserController::list' ]
];
$result = $this->withRoutes($routes)
->get('users');
每個(gè)“路由”都是一個(gè)3元素?cái)?shù)組,包含HTTP動(dòng)詞(或全部為“添加”),要匹配的URI和路由目的地。
您可以使用方法設(shè)置自定義會(huì)話值,以在單個(gè)測(cè)試中使用withSession()
。發(fā)出此請(qǐng)求時(shí),這需要鍵/值對(duì)的數(shù)組,這些鍵/值對(duì)應(yīng)存在于$ _SESSION變量中。這對(duì)于測(cè)試身份驗(yàn)證非常方便。
$values = [
'logged_in' => 123
];
$result = $this->withSession($values)
->get('admin');
事件很容易在您的應(yīng)用程序中使用,但在測(cè)試過(guò)程中可能會(huì)出現(xiàn)問(wèn)題。尤其是用于發(fā)送電子郵件的事件。您可以使用以下skipEvents()
方法告訴系統(tǒng)跳過(guò)任何事件處理:
$result = $this->skipEvents()
->post('users', $userInfo);
執(zhí)行一個(gè) call()
并獲得結(jié)果后,可以在測(cè)試中使用許多新的斷言。
注解
響應(yīng)對(duì)象可從公開(kāi)獲得$result-&response
。如果需要,可以使用該實(shí)例對(duì)它執(zhí)行其他聲明。
isOK()
根據(jù)響應(yīng)是否被認(rèn)為是“ ok”,返回布爾值true / false。這主要由200或300的響應(yīng)狀態(tài)代碼確定。
if ($result->isOK())
{
...
}
assertOK()
該論斷僅使用isOK()方法來(lái)測(cè)試響應(yīng)。
$this->assertOK();
isRedirect()
根據(jù)響應(yīng)是否為重定向響應(yīng),返回布爾值true / false。
if ($result->isRedirect())
{
...
}
assertRedirect()
論斷該響應(yīng)是RedirectResponse的一個(gè)實(shí)例。
$this->assertRedirect();
assertStatus(int $code)
論斷返回的HTTP狀態(tài)代碼與$ code相匹配。
$this->assertStatus(403);
assertSessionHas(string $key, $value = null)
斷言結(jié)果會(huì)話中存在值。如果傳遞了$ value,還將斷言該變量的值與指定的值匹配。
$this->assertSessionHas('logged_in', 123);
assertSessionMissing(string $key)
斷言結(jié)果會(huì)話不包含指定的$ key。
$this->assertSessionMissin('logged_in');
assertHeader(string $key, $value = null)
斷言響應(yīng)中存在名為$ key的標(biāo)頭。如果$ value不為空,還將斷言這些值匹配。
$this->assertHeader('Content-Type', 'text/html');
assertHeaderMissing(string $key)
斷言響應(yīng)中不存在標(biāo)頭名稱$ key。
$this->assertHeader('Accepts');
assertCookie(string $key, $value = null, string $prefix = ‘’)
斷言響應(yīng)中存在一個(gè)名為$ key的cookie 。如果$ value不為空,還將斷言這些值匹配。您可以根據(jù)需要通過(guò)將cookie前綴作為第三個(gè)參數(shù)傳遞來(lái)設(shè)置它。
$this->assertCookie('foo', 'bar');
assertCookieMissing(string $key)
斷言響應(yīng)中不存在名為$ key的cookie 。
$this->assertCookieMissing('ci_session');
assertCookieExpired(string $key, string $prefix = ‘’)
斷言存在一個(gè)名為$ key的cookie ,但已過(guò)期。您可以根據(jù)需要通過(guò)將cookie前綴作為第二個(gè)參數(shù)傳遞來(lái)設(shè)置它。
$this->assertCookieExpired('foo');
您可以執(zhí)行測(cè)試,以查看帶有以下聲明的響應(yīng)的正文中是否存在特定的元素/文本/等。
assertSee(string $search = null, string $element = null)
斷言文本/ HTML是否在頁(yè)面上,無(wú)論是本身,還是(更具體而言)在標(biāo)簽內(nèi),由類型,類或id指定:
// Check that "Hello World" is on the page
$this->assertSee('Hello World');
// Check that "Hello World" is within an h1 tag
$this->assertSee('Hello World', 'h1');
// Check that "Hello World" is within an element with the "notice" class
$this->assertSee('Hello World', '.notice');
// Check that "Hello World" is within an element with id of "title"
$this->assertSee('Hellow World', '#title');
assertDontSee(string $search = null, string $element = null)
聲明與assertSee()方法完全相反的地方:
// Checks that "Hello World" does NOT exist on the page
$results->dontSee('Hello World');
// Checks that "Hello World" does NOT exist within any h1 tag
$results->dontSee('Hello World', 'h1');
assertSeeElement(string $search)
與assertSee()類似,但是這僅檢查現(xiàn)有元素。它不檢查特定的文本:
// Check that an element with class 'notice' exists
$results->seeElement('.notice');
// Check that an element with id 'title' exists
$results->seeElement('#title')
assertDontSeeElement(string $search)
與assertSee()類似,但是這僅檢查缺少的現(xiàn)有元素。它不檢查特定的文本:
// Verify that an element with id 'title' does NOT exist
$results->dontSeeElement('#title');
assertSeeLink(string $text, string $details=null)
斷言找到一個(gè)匹配標(biāo)簽為$ text的錨標(biāo)簽:
// Check that a link exists with 'Upgrade Account' as the text::
$results->seeLink('Upgrade Account');
// Check that a link exists with 'Upgrade Account' as the text, AND a class of 'upsell'
$results->seeLink('Upgrade Account', '.upsell');
assertSeeInField(string $field, string $value=null)
斷言輸入標(biāo)簽具有名稱和值:
// Check that an input exists named 'user' with the value 'John Snow'
$results->seeInField('user', 'John Snow');
// Check a multi-dimensional input
$results->seeInField('user[name]', 'John Snow');
響應(yīng)通常包含JSON響應(yīng),尤其是在使用API方法時(shí)。以下方法可以幫助測(cè)試響應(yīng)。
getJSON()
此方法將以JSON字符串的形式返回響應(yīng)的主體:
// Response body is this:
['foo' => 'bar']
$json = $result->getJSON();
// $json is this:
{
"foo": "bar"
}
注解
請(qǐng)注意,JSON字符串將漂亮地打印在結(jié)果中。
assertJSONFragment(array $fragment)
斷言$fragment在JSON響應(yīng)中找到。它不需要匹配整個(gè)JSON值。
// Response body is this:
[
'config' => ['key-a', 'key-b']
]
// Is true
$this->assertJSONFragment(['config' => ['key-a']);
注解
這只是使用phpUnit自己的assertArraySubset() 方法進(jìn)行比較。
assertJSONExact($test)
與assertJSONFragment()相似,但是檢查整個(gè)JSON響應(yīng)以確保完全匹配。
getXML()
如果您的應(yīng)用程序返回XML,則可以通過(guò)此方法檢索它。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: