A macro test helper to help you to write integration tests.
Basically:
fn setup()
before your testfn teardown()
after the test endAdd the following to your Cargo.toml
yaml
[dependencies]
tearup = "0.2"
The macros #[tearup(MyContext)]
executes the setup
then the method annoted and finally the teardown
.
The #[tearup_test(MyContext)]
act the same + add the #[test]
on the method.
```rust
fn itshoulddo_this(address: Address) { // assert something }
async fn itshoulddo_that(mut db: DbConnection, address: Address) { // assert something } ```
To do this you'll need to implement Context
trait with both setup
and teardown
methods.
```rust use asynctrait::asynctrait; use tearup::tearup_test;
// Define your context struct YourContext { somethingyouneedinteardown: SomethingYouSetup, }
impl Context for YourContext { async fn setup(shared_context: &mut SharedContext) -> Self { /* --> do your setup here <-- */
// Register struct that you want to access in your test
shared_context.register(SomethingYouNeedInTest{});
// You still can store things in your struct for the treardown step
Self { something_you_need_in_teardown: SomethingYouSetup{} }
}
async fn teardown(mut self, shared_context: &mut SharedContext) {
/* --> clean your stuff here <-- */
// You still have access to the shared context
shared_context.get::<SomethingYouNeedInTest>();
// Same for self
self.something_you_need_in_teardown;
}
}
/// Type you need to access in test (registered in the SharedContext) must implement Clone
struct SomethingYouNeedInTest;
struct SomethingYouSetup; ```
You can also combine your contexts with ContextCombinator
:
```rust
type Both = ConcurrentContextCombinator
fn itshoulddothis(mut somethingyouneedintest: DbConnection, somethingfromtheother_context: Address) { // assert something }
type MoreCombinaison = ConcurrentContextCombinator
fn itshoulddothat(mut somethingyouneedintest: DbConnection, somethingfromtheother_context: Address) { // assert something } ```