Testing
Fluent assertions over Inertia responses and helpers for partial reloads and deferred props.
On this page
Inertia.Net.Testing has Laravel-style fluent assertions over Inertia responses and HttpClient helpers. It parses both JSON responses (X-Inertia: true) and the first-visit HTML <script data-page>, and decodes $bigint values. Failures throw InertiaAssertionException with a readable message, so it works with any test framework.
Add public partial class Program; to your app when it uses top-level statements.
To check that an error page keeps its HTTP status, use Status with the response's page assertion:
Request helpers
Page assertions
Component, Url, Version, HasFlash(key), HasFlash(key, value), MissingFlash(key), HasDeferred(path), HasDeferred(group, path), HasLive(path) and HasLive(path, channel, event) for live props (the channel as "{type}:{name}", e.g. private:orders.1).
Prop assertions
On props and nested scopes (dot paths and array indexes allowed):
Has(key), Has(key, count), Has(key, scope), Has(key, count, firstItemScope), HasAll, HasAny, Missing, MissingAll, Where(key, value), Where(key, predicate), WhereNot, WhereNull, WhereNotNull, WhereType(key, "string|null"), WhereAllType, WhereContains(key, ...), Count, CountBetween, Each(scope), First(scope) and Dump().
Strict interaction tracking. Like Laravel, a nested scope fails when it contains properties you did not assert, unless you end it with Etc(). The root scope is not checked automatically: call .Interacted() at the end to enforce that every top-level prop was asserted.
Partial reloads and deferred props
InertiaPage replays the request with the right headers:
HasScroll(path) asserts scroll metadata; HasScroll(path, currentPage, nextPage) also checks the page values. The page helpers use the metadata and send the appropriate append or prepend intent.
ReloadOnlyAsync asserts that "users" is in the result; ReloadExceptAsync asserts that it is missing.
InertiaPage members
Props, Raw, Prop(path), HasProp(path), PropBigInteger(path), DeferredProps, MergeProps, PrependProps, DeepMergeProps, MatchPropsOn, RescuedProps, SharedProps, ScrollProps, OnceProps, LiveProps, Flash, EncryptHistory, ClearHistory, PreserveFragment, PreserveBigIntegers.
Cookies and redirects
Flash and errors travel in a cookie across the redirect, so use a client that keeps cookies. Disable AllowAutoRedirect to assert the redirect itself, then follow it with a second InertiaGetAsync to read the flash or errors.
Testing a form with antiforgery
Keep cookies and disable redirects on the test client. Load the form first, then send its XSRF token on the Inertia POST and follow the redirect:
Import System.Net and System.Net.Http.Json. For a named bag use HasErrorBag("createUser") and HasError("email", bag: "createUser"). Dotted keys such as items.0.email are matched as flat error keys; with all errors enabled a message assertion matches any message. GetXsrfToken() and WithXsrfToken() accept custom cookie and header names. The cookie container must retain the antiforgery cookie as well as the Inertia state cookie.