Skip to main content

TestApp

Struct TestApp 

Source
pub struct TestApp { /* private fields */ }
Expand description

The app under test, with a browser-like client.

Implementations§

Source§

impl TestApp

Source

pub async fn new(app: App) -> Self

Boots app with the test configuration and runs its migrations.

Source

pub async fn with_config(app: App, configure: impl FnOnce(&mut Config)) -> Self

Like new, after configure adjusts the test configuration.

Source

pub fn kernel(&self) -> &Kernel

The booted app behind this TestApp.

Source

pub fn state(&self) -> &AppState

The app’s shared state.

Source

pub fn db(&self) -> &Db

The app’s database, migrated at start.

Source

pub fn mailer(&self) -> &Mailer

The app’s mailer (the memory driver unless the config changes it).

Source

pub fn fake_http(&self) -> FakeHttp

Answers state.http requests with fakes and records them, instead of reaching the network; see crate::http::FakeHttp.

Source

pub fn sent_mail(&self) -> Vec<Mail>

Mail sent so far.

Source

pub async fn run_jobs(&self) -> usize

Runs the jobs queued so far; returns how many ran.

Source

pub async fn run_scheduled(&self, name: &str) -> Result

Runs the scheduled task name now (as schedule:run does), at the time TestApp::travel moved the clock to.

app.travel(Duration::from_secs(31 * 24 * 60 * 60)); // a month later
app.run_scheduled("monthly-report").await?;
Source

pub fn travel(&self, by: Duration) -> &Self

Moves the clock forward by by for what this TestApp does next: requests, run_jobs, run_scheduled, and code run in TestApp::at_travelled_time (renox::db::now(), sessions, signed URLs, the queue, the cache). Adds up; TestApp::travel_back returns to the present.

// The app guards `/settings/billing` with `.require_password_confirmed()`.
app.acting_as(&user).confirm_password();
app.travel(Duration::from_secs(3 * 60 * 60)); // past the password confirmation
app.get("/settings/billing").await.assert_redirect("/confirm-password");
Source

pub fn travel_back(&self) -> &Self

Returns the clock to the present.

Source

pub async fn at_travelled_time<F: Future>(&self, fut: F) -> F::Output

Runs fut with the clock where TestApp::travel moved it, e.g. to call a model or a job’s code directly.

Source

pub fn fake_events(&self) -> &Self

Records events instead of running their listeners, from now on; read them with TestApp::emitted or TestApp::assert_emitted.

Source

pub fn emitted<E: Event>(&self) -> Vec<E>

The events of type E emitted since fake_events, oldest first.

Source

pub fn assert_emitted<E: Event>(&self, check: impl Fn(&E) -> bool) -> &Self

Panics unless an E matching check was emitted.

Source

pub fn assert_not_emitted<E: Event>(&self) -> &Self

Panics unless no E was emitted since fake_events.

Source

pub fn fake_notifications(&self) -> &Self

Records notifications instead of sending them (mail, database, channels), from now on.

Source

pub fn notifications(&self) -> Vec<SentNotification>

The notifications recorded since fake_notifications, oldest first.

Source

pub fn assert_notified(&self, user: &User, kind: &str) -> &Self

Panics unless user was sent a notification of kind.

Source

pub fn assert_notified_to(&self, address: &str, kind: &str) -> &Self

Panics unless someone with address (on any channel, or a user’s email) was sent a notification of kind.

Source

pub fn fake_broadcasts(&self) -> &Self

Records what state.broadcast(…) and broadcast_to(…) send from now on, instead of sending it to the open pages.

Source

pub fn broadcasts(&self) -> Vec<SentBroadcast>

The broadcasts recorded since fake_broadcasts, oldest first.

Source

pub fn assert_broadcast( &self, event: &str, check: impl Fn(&SentBroadcast) -> bool, ) -> &Self

Panics unless an event whose data passes check was broadcast (to anyone) since fake_broadcasts.

Source

pub fn assert_nothing_notified(&self) -> &Self

Panics unless no notification was recorded since fake_notifications.

Source

pub fn session_get<T: DeserializeOwned>(&self, key: &str) -> Option<T>

A value in the session as the next request will see it.

Source

pub fn assert_session_has(&self, key: &str) -> &Self

Panics unless the session holds key.

Source

pub fn assert_session_missing(&self, key: &str) -> &Self

Panics if the session holds key.

Source

pub fn assert_authenticated(&self, user: Option<&User>) -> &Self

Panics unless the session is logged in (as user, when given).

Source

pub fn assert_guest(&self) -> &Self

Panics unless the session is a guest’s (no one logged in).

Source

pub async fn serve(&self) -> String

Serves the app on a free local port and returns its base URL (http://127.0.0.1:…), e.g. for a browser test (docs/testing.md). The server runs until the test’s runtime ends.

Source

pub async fn run_all_jobs(&self) -> usize

Runs every queued job, including those waiting for a delay or a retry’s backoff, until none is left (at most 1,000 rounds); returns how many attempts ran. For tests of retries and failed hooks.

Source

pub async fn queued_jobs(&self) -> Vec<String>

Names of the jobs waiting in the queue, oldest first.

Source

pub fn acting_as(&self, user: &User) -> &Self

Logs user in for the following requests.

Source

pub fn confirm_password(&self) -> &Self

Marks the password as just typed, so routes behind require_password_confirmed let the user through.

Source

pub fn logout(&self) -> &Self

Forgets the session, like a browser with cookies cleared.

The session cookie as the browser holds it (name=value), e.g. to play a second device: save one, log in again, switch back with TestApp::use_session_cookie.

Sends this session cookie from now on (see TestApp::session_cookie).

Source

pub fn csrf_token(&self) -> String

The CSRF token of the current session, creating the session if needed.

Source

pub fn request(&self) -> TestRequest<'_>

A request with extra headers, e.g. app.request().htmx().post(..).

Source

pub fn htmx(&self) -> TestRequest<'_>

Shorthand for request().htmx().

Source

pub async fn get(&self, uri: &str) -> TestResponse

A GET request.

Source

pub async fn post(&self, uri: &str, form: &[(&str, &str)]) -> TestResponse

A form post (urlencoded), with the CSRF token.

Source

pub async fn put(&self, uri: &str, form: &[(&str, &str)]) -> TestResponse

A form PUT (urlencoded), with the CSRF token.

Source

pub async fn patch(&self, uri: &str, form: &[(&str, &str)]) -> TestResponse

A form PATCH (urlencoded), with the CSRF token.

Source

pub async fn delete(&self, uri: &str) -> TestResponse

A DELETE request, with the CSRF token.

Source

pub async fn post_json(&self, uri: &str, body: &impl Serialize) -> TestResponse

A JSON post, with the CSRF token.

Source

pub async fn post_multipart( &self, uri: &str, fields: &[(&str, &str)], files: &[(&str, &str, &[u8])], ) -> TestResponse

A multipart form with files; see TestRequest::post_multipart.

Source

pub async fn post_body( &self, uri: &str, content_type: &str, body: impl Into<Vec<u8>>, ) -> TestResponse

A POST with exactly these bytes (e.g. a signed webhook); no CSRF token.

Source

pub async fn assert_database_has( &self, table: &str, values: &[(&str, &(dyn ToDbValue + Sync))], )

Fails unless a row of table has all these column values.

Source

pub async fn assert_database_missing( &self, table: &str, values: &[(&str, &(dyn ToDbValue + Sync))], )

Fails if a row of table has all these column values.

Source

pub async fn assert_database_count(&self, table: &str, expected: i64)

Fails unless table has exactly expected rows.

Source

pub fn assert_mail_sent(&self, to: &str, subject: &str)

Fails unless a mail went to to with subject in its subject.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Fake for T

Source§

fn fake<U>(&self) -> U
where Self: FakeBase<U>,

Source§

fn fake_with_rng<U, R>(&self, rng: &mut R) -> U
where R: RngExt + ?Sized, Self: FakeBase<U>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more