Skip to main content

TestRunner

Struct TestRunner 

Source
pub struct TestRunner<A: App> { /* private fields */ }
Expand description

A test runner for TEA applications

Provides a convenient way to test update logic without running the UI.

Implementations§

Source§

impl<A: App> TestRunner<A>

Source

pub fn new() -> Self

Create a new test runner with initial model

Source

pub fn with_model(model: A::Model) -> Self

Create a test runner with a custom initial model

Source

pub fn send(&mut self, msg: A::Msg) -> &mut Self

Send a message and process the update

Source

pub fn send_all(&mut self, msgs: impl IntoIterator<Item = A::Msg>) -> &mut Self

Send multiple messages in sequence

Source

pub fn model(&self) -> &A::Model

Get a reference to the current model

Source

pub fn model_mut(&mut self) -> &mut A::Model

Get a mutable reference to the model (for setup)

Source

pub fn last_cmd(&self) -> Option<&CmdRecord<A::Msg>>

Get the last command record

Source

pub fn commands(&self) -> &[CmdRecord<A::Msg>]

Get all command records

Source

pub fn clear_commands(&mut self) -> &mut Self

Clear command history

Source

pub fn last_was_none(&self) -> bool

Check if the last command was Cmd::None

Source

pub fn last_was_task(&self) -> bool

Check if the last command was Cmd::Task

Source

pub fn last_was_msg(&self) -> bool

Check if the last command was Cmd::Msg

Source

pub fn pending_task_count(&self) -> usize

Get the number of pending async tasks

Source

pub fn has_pending_tasks(&self) -> bool

Check if there are any pending async tasks

Source

pub async fn process_task(&mut self) -> bool

Process one pending async task

Executes the first pending task, awaits its result, and sends the resulting message through update.

Returns true if a task was processed, false if no tasks were pending.

§Example
runner.send(Msg::FetchData);
assert!(runner.has_pending_tasks());

runner.process_task().await;
assert!(!runner.has_pending_tasks());
Source

pub async fn process_tasks(&mut self) -> &mut Self

Process all pending async tasks

Processes tasks until none remain. Note that processing a task may add new tasks (if the resulting message produces new Cmd::Task), so this processes until the queue is fully drained.

§Example
runner.send(Msg::FetchData);
runner.process_tasks().await;
// All tasks completed, results sent through update
Source

pub async fn process_n_tasks(&mut self, n: usize) -> &mut Self

Process exactly N pending tasks

Useful when you want to control the order of task execution or test intermediate states.

§Panics

Panics if there are fewer than N pending tasks.

Source

pub fn expect_model( &mut self, predicate: impl FnOnce(&A::Model) -> bool, ) -> &mut Self

Assert that the model satisfies a predicate

§Example
runner
    .send(Msg::Inc)
    .expect_model(|m| m.count == 1)
    .send(Msg::Inc)
    .expect_model(|m| m.count == 2);
§Panics

Panics if the predicate returns false

Source

pub fn expect_model_msg( &mut self, predicate: impl FnOnce(&A::Model) -> bool, msg: &str, ) -> &mut Self

Assert that the model satisfies a predicate with custom message

§Panics

Panics with the provided message if the predicate returns false

Source

pub fn expect_cmd_none(&mut self) -> &mut Self

Assert that the last command was Cmd::None

§Example
runner
    .send(Msg::SetValue(42))
    .expect_cmd_none();
§Panics

Panics if the last command was not Cmd::None

Source

pub fn expect_cmd_task(&mut self) -> &mut Self

Assert that the last command was Cmd::Task

§Example
runner
    .send(Msg::FetchData)
    .expect_cmd_task();
§Panics

Panics if the last command was not Cmd::Task

Source

pub fn expect_cmd_msg(&mut self) -> &mut Self

Assert that the last command was Cmd::Msg

§Example
runner
    .send(Msg::TriggerDelayed)
    .expect_cmd_msg();
§Panics

Panics if the last command was not Cmd::Msg

Source

pub fn expect_cmd_msg_eq(&mut self, expected: A::Msg) -> &mut Self
where A::Msg: PartialEq + Debug,

Assert that the last command was Cmd::Msg and verify its content

§Example
runner
    .send(Msg::TriggerDelayed)
    .expect_cmd_msg_eq(Msg::Inc);
§Panics

Panics if the last command was not Cmd::Msg or the message doesn’t match

Source

pub fn expect_cmd_batch(&mut self) -> &mut Self

Assert that the last command was Cmd::Batch

§Example
runner
    .send(Msg::MultiAction)
    .expect_cmd_batch();
§Panics

Panics if the last command was not Cmd::Batch

Source

pub fn expect_cmd_batch_size(&mut self, expected_size: usize) -> &mut Self

Assert that the last command was Cmd::Batch with expected size

§Example
runner
    .send(Msg::MultiAction)
    .expect_cmd_batch_size(3);
§Panics

Panics if the last command was not Cmd::Batch or size doesn’t match

Trait Implementations§

Source§

impl<A: App> Default for TestRunner<A>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<A: App> ModelAssert<<A as App>::Model> for TestRunner<A>

Source§

fn assert_that(&self, predicate: impl FnOnce(&A::Model) -> bool, msg: &str)

Assert with a predicate

Auto Trait Implementations§

§

impl<A> Freeze for TestRunner<A>
where <A as App>::Model: Freeze,

§

impl<A> !RefUnwindSafe for TestRunner<A>

§

impl<A> Send for TestRunner<A>

§

impl<A> !Sync for TestRunner<A>

§

impl<A> Unpin for TestRunner<A>
where <A as App>::Model: Unpin, <A as App>::Msg: Unpin,

§

impl<A> UnsafeUnpin for TestRunner<A>
where <A as App>::Model: UnsafeUnpin,

§

impl<A> !UnwindSafe for TestRunner<A>

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<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