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>
impl<'a> StepContext<'a>
Sourcepub fn owned_cell<T: Any>(value: T) -> RefCell<Box<dyn Any>>
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.
Sourcepub fn insert<T: Any>(&mut self, name: &'static str, value: &'a T)
pub fn insert<T: Any>(&mut self, name: &'static str, value: &'a T)
Insert a fixture reference by name.
Sourcepub fn insert_owned<T: Any>(
&mut self,
name: &'static str,
cell: &'a RefCell<Box<dyn Any>>,
)
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.
Sourcepub fn get<T: Any>(&'a self, name: &str) -> Option<&'a T>
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.
Sourcepub fn borrow_ref<'b, T: Any>(&'b self, name: &str) -> Option<FixtureRef<'b, T>>where
'a: 'b,
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.
Sourcepub fn borrow_mut<'b, T: Any>(
&'b mut self,
name: &str,
) -> Option<FixtureRefMut<'b, T>>where
'a: 'b,
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.
Sourcepub fn available_fixtures(&self) -> impl Iterator<Item = &'static str> + '_
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"));Sourcepub fn insert_value(&mut self, value: Box<dyn Any>) -> Option<Box<dyn Any>>
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.