pub struct Fixture { /* private fields */ }Expand description
A saved state for tests to start from, in place of the app’s freshly provisioned one: the rows a setup inserted, say.
use encore::testing::Fixture;
static SEEDED: Fixture = Fixture::new("seeded", || Box::pin(seed()));
async fn seed() -> Result<(), encore::Error> {
// Ordinary app code: its resources are the fixture's while it is built.
DB.execute("INSERT INTO users (name) VALUES ('ada'), ('grace')", &[]).await?;
Ok(())
}
#[tokio::test]
async fn lists_users() -> Result<(), encore::Error> {
SEEDED.load().await?;
assert_eq!(list_users().await?.len(), 2);
Ok(())
}The setup runs once per test run, the first time a test loads the fixture, however many tests, threads and test binaries load it. What it leaves in each resource it touched is saved; a test that loads the fixture gets a copy of its own, to change as it likes. A resource the setup did not touch starts as it would without the fixture — or, in a fixture that extends another, as that one left it.
Databases, buckets and Redis databases are saved. The other kinds of resource start a test from their initial state, with or without a fixture.
A fixture is kept from one test run to the next for as long as the app’s source is unchanged, so its setup need not run again — unless it touched a Redis database, which lasts only as long as the run: such a fixture is set up again each run.
The fixture! macro declares one with less ceremony, and
can have a fixture loaded by every #[encore::test] in a module.
Implementations§
Source§impl Fixture
impl Fixture
Sourcepub const fn new(name: &'static str, setup: SetupFn) -> Self
pub const fn new(name: &'static str, setup: SetupFn) -> Self
Declares a fixture. const, so it can initialize a static.
The name identifies the fixture across the app’s test binaries: two fixtures of one name are one fixture, built by whichever is loaded first.
Auto Trait Implementations§
impl !Freeze for Fixture
impl !RefUnwindSafe for Fixture
impl !UnwindSafe for Fixture
impl Forget for Fixture
impl Move for Fixture
impl Send for Fixture
impl Sync for Fixture
impl Unpin for Fixture
impl UnsafeUnpin for Fixture
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request