有許多不同的方法可以測試 Inertia 應用程式。此頁面提供可用工具的快速概觀。
測試 JavaScript 頁面元件的一種常見方法是使用端到端測試工具,例如 Cypress 或 Laravel Dusk。這些是瀏覽器自動化工具,允許您在瀏覽器中執行應用程式的真實模擬。這些測試已知速度較慢;但是,由於它們在與最終用戶相同的層級測試您的應用程式,因此它們可以提供很大的信心,確保您的應用程式正常運作。而且,由於這些測試是在瀏覽器中執行的,因此您的 JavaScript 程式碼也會實際執行和測試。
測試頁面元件的另一種方法是使用用戶端單元測試框架,例如 Jest 或 Mocha。此方法允許您使用 Node.js 隔離測試您的 JavaScript 頁面元件。
除了測試 JavaScript 頁面元件之外,您可能還需要測試伺服器端框架傳回的 Inertia 回應。一種常見的方法是使用端點測試,您向應用程式發出請求並檢查回應。Laravel 提供工具 來執行這些類型的測試。
但是,為了使此過程更容易,Inertia 的 Laravel 適配器提供了額外的 HTTP 測試工具。讓我們看看一個例子。
use Inertia\Testing\AssertableInertia as Assert;
class PodcastsControllerTest extends TestCase
{
public function test_can_view_podcast()
{
$this->get('/podcasts/41')
->assertInertia(fn (Assert $page) => $page
->component('Podcasts/Show')
->has('podcast', fn (Assert $page) => $page
->where('id', $podcast->id)
->where('subject', 'The Laravel Podcast')
->where('description', 'The Laravel Podcast brings you Laravel and PHP development news and discussion.')
->has('seasons', 4)
->has('seasons.4.episodes', 21)
->has('host', fn (Assert $page) => $page
->where('id', 1)
->where('name', 'Matt Stauffer')
)
->has('subscribers', 7, fn (Assert $page) => $page
->where('id', 2)
->where('name', 'Claudio Dekker')
->where('platform', 'Apple Podcasts')
->etc()
->missing('email')
->missing('password')
)
)
);
}
}
如您在上面的範例中看到的,您可以使用這些斷言方法來斷言 Inertia 回應提供的資料內容。此外,您還可以斷言陣列資料具有給定的長度以及範圍您的斷言。
讓我們詳細探討可用的斷言。首先,要斷言 Inertia 回應是否具有屬性,您可以使用 has
方法。您可以將此方法視為類似於 PHP 的 isset
函數。
$response->assertInertia(fn (Assert $page) => $page
// Checking a root-level property...
->has('podcast')
// Checking nested properties using "dot" notation...
->has('podcast.id')
);
要斷言 Inertia 屬性是否具有指定的項目數量,您可以將預期的尺寸作為第二個參數提供給 has
方法。
$response->assertInertia(fn (Assert $page) => $page
// Checking if a root-level property has 7 items...
->has('podcasts', 7)
// Checking nested properties using "dot" notation...
->has('podcast.subscribers', 7)
);
has
方法也可用于範圍屬性,以便在對巢狀屬性斷言時減少重複。
$response->assertInertia(fn (Assert $page) => $page
// Creating a single-level property scope...
->has('message', fn (Assert $page) => $page
// We can now continue chaining methods...
->has('subject')
->has('comments', 5)
// And can even create a deeper scope using "dot" notation...
->has('comments.0', fn (Assert $page) => $page
->has('body')
->has('files', 1)
->has('files.0', fn (Assert $page) => $page
->has('url')
)
)
)
);
當範圍到是陣列或集合的 Inertia 屬性時,除了範圍到第一個項目之外,您還可以斷言是否存在指定的項目數。
$response->assertInertia(fn (Assert $page) => $page
// Assert that there are 5 comments and automatically scope into the first comment...
->has('comments', 5, fn (Assert $page) => $page
->has('body')
// ...
)
);
要斷言 Inertia 屬性是否具有預期的值,您可以使用 where
斷言。
$response->assertInertia(fn (Assert $page) => $page
->has('message', fn (Assert $page) => $page
// Assert that the subject prop matches the given message...
->where('subject', 'This is an example message')
// Or, assert against deeply nested values...
->where('comments.0.files.0.name', 'example-attachment.pdf')
)
);
當您沒有與範圍內的至少一個 props 互動時,Inertia 的測試方法會自動失敗。雖然這通常很有用,但您可能會遇到正在處理不可靠資料(例如來自外部供稿)或您真的不想與之互動以保持測試簡單的資料的情況。對於這些情況,etc
方法存在。
$response->assertInertia(fn (Assert $page) => $page
->has('message', fn (Assert $page) => $page
->has('subject')
->has('comments')
->etc()
)
);
missing
方法與 has
方法完全相反,確保屬性不存在。此方法是 etc
方法的一個很好的補充。
$response->assertInertia(fn (Assert $page) => $page
->has('message', fn (Assert $page) => $page
->has('subject')
->missing('published_at')
->etc()
)
);