Skip to main content

StepContext

Struct StepContext 

Source
pub struct StepContext<'a> { /* private fields */ }
Expand description

Context passed to step functions containing references to requested fixtures.

This is constructed by the #[scenario] macro for each step invocation. Use insert_owned when a fixture should be shared mutably across steps; step functions may then request &mut T and mutate world state without resorting to interior mutability wrappers.

§Examples

use rstest_bdd::StepContext;

let mut ctx = StepContext::default();
let value = 42;
ctx.insert("my_fixture", &value);
let owned = StepContext::owned_cell(String::from("hi"));
ctx.insert_owned::<String>("owned", &owned);

let retrieved: Option<&i32> = ctx.get("my_fixture");
assert_eq!(retrieved, Some(&42));
{
    let mut suffix = ctx.borrow_mut::<String>("owned").expect("owned fixture");
    suffix.value_mut().push('!');
}
drop(ctx);
let owned = owned.into_inner();
let owned: String = *owned
    .downcast::<String>()
    .expect("fixture should downcast to String");
assert_eq!(owned, "hi!");

Implementations§

Source§

impl<'a> StepContext<'a>

Source

pub fn owned_cell<T: Any>(value: T) -> RefCell<Box<dyn Any>>

Create an owned fixture cell for use with insert_owned.

This helper boxes the provided value and erases its concrete type so it can back a mutable fixture. Callers must retain the returned cell for as long as the context references it.

Source

pub fn insert<T: Any>(&mut self, name: &'static str, value: &'a T)

Insert a fixture reference by name.

Source

pub fn insert_owned<T: Any>( &mut self, name: &'static str, cell: &'a RefCell<Box<dyn Any>>, )

Insert a fixture backed by a RefCell<Box<dyn Any>>, enabling mutable borrows.

A runtime type check ensures the stored value matches the requested T so mismatches are surfaced immediately instead of silently failing at borrow time.

§Panics

Panics when the provided cell does not currently store a value of type T, because continuing would render the fixture un-borrowable at run time.

Source

pub fn get<T: Any>(&'a self, name: &str) -> Option<&'a T>

Retrieve a fixture reference by name and type.

Values returned from prior #[when] steps override fixtures of the same type when that type is unique among fixtures. This enables a functional style where step return values feed into later assertions without having to define ad-hoc fixtures.

Source

pub fn borrow_ref<'b, T: Any>(&'b self, name: &str) -> Option<FixtureRef<'b, T>>
where 'a: 'b,

Borrow a fixture by name, keeping the guard alive until dropped.

Source

pub fn borrow_mut<'b, T: Any>( &'b mut self, name: &str, ) -> Option<FixtureRefMut<'b, T>>
where 'a: 'b,

Borrow a fixture mutably by name.

§Panics

The underlying fixtures use RefCell for interior mutability. Attempting to borrow the same fixture mutably while an existing mutable guard is alive will panic via RefCell::borrow_mut. Callers must drop guards before requesting another mutable borrow of the same fixture.

Source

pub fn available_fixtures(&self) -> impl Iterator<Item = &'static str> + '_

Returns an iterator over the names of all available fixtures.

This method is useful for diagnostic purposes, such as generating error messages that list which fixtures are available when a required fixture is missing.

§Examples
use rstest_bdd::StepContext;

let mut ctx = StepContext::default();
let value = 42;
ctx.insert("my_fixture", &value);

let names: Vec<_> = ctx.available_fixtures().collect();
assert!(names.contains(&"my_fixture"));
Source

pub fn insert_value(&mut self, value: Box<dyn Any>) -> Option<Box<dyn Any>>

Insert a value produced by a prior step. The value overrides a fixture only if exactly one fixture has the same type; otherwise it is ignored to avoid ambiguity.

Returns the previous override for that fixture when one existed.

Trait Implementations§

Source§

impl<'a> Default for StepContext<'a>

Source§

fn default() -> StepContext<'a>

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

Auto Trait Implementations§

§

impl<'a> Freeze for StepContext<'a>

§

impl<'a> !RefUnwindSafe for StepContext<'a>

§

impl<'a> !Send for StepContext<'a>

§

impl<'a> !Sync for StepContext<'a>

§

impl<'a> Unpin for StepContext<'a>

§

impl<'a> !UnwindSafe for StepContext<'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, 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> 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 = 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.