Merge pull request #10 from markhildreth/add-sync-async-impl-docs

Documented behavior of AsyncContext with normal function.
This commit is contained in:
Mark Hildreth 2021-02-23 21:06:53 -05:00 committed by GitHub
commit 121982ef72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 7 deletions

View file

@ -31,9 +31,8 @@ fn test_works(ctx: &mut MyContext) {
}
```
Works with other test wrappers like [`actix_rt::test`](https://docs.rs/actix-rt/1.1.1/actix_rt/attr.test.html) or
[`tokio::test`](https://docs.rs/tokio/1.0.2/tokio/attr.test.html) that turn your test function into an async
function.
Alternatively, you can use `async` functions in your test context by using the
`AsyncTestContext`.
```rust
use test_context::{test_context, AsyncTestContext};
@ -53,6 +52,17 @@ impl AsyncTestContext for MyAsyncContext {
}
}
#[test_context(MyAsyncContext)]
fn test_works(ctx: &mut MyAsyncContext) {
assert_eq!(ctx.value, "Hello, World!");
}
```
The `AsyncTestContext` works well with async test wrappers like
[`actix_rt::test`](https://docs.rs/actix-rt/1.1.1/actix_rt/attr.test.html) or
[`tokio::test`](https://docs.rs/tokio/1.0.2/tokio/attr.test.html).
```rust
#[test_context(MyAsyncContext)]
#[tokio::test]
async fn test_works(ctx: &mut MyAsyncContext) {

View file

@ -24,9 +24,8 @@
//! }
//! ```
//!
//! Works with other test wrappers like [`actix_rt::test`](https://docs.rs/actix-rt/1.1.1/actix_rt/attr.test.html) or
//! [`tokio::test`](https://docs.rs/tokio/1.0.2/tokio/attr.test.html) that turn your test function into an async
//! function.
//! Alternatively, you can use `async` functions in your test context by using the
//! `AsyncTestContext`.
//!
//! ```
//! use test_context::{test_context, AsyncTestContext};
@ -47,8 +46,32 @@
//! }
//!
//! #[test_context(MyAsyncContext)]
//! fn test_works(ctx: &mut MyAsyncContext) {
//! assert_eq!(ctx.value, "Hello, World!");
//! }
//! ```
//!
//! The `AsyncTestContext` works well with async test wrappers like
//! [`actix_rt::test`](https://docs.rs/actix-rt/1.1.1/actix_rt/attr.test.html) or
//! [`tokio::test`](https://docs.rs/tokio/1.0.2/tokio/attr.test.html).
//!
//! ```
//! # use test_context::{test_context, AsyncTestContext};
//! # struct MyAsyncContext {
//! # value: String
//! # }
//! # #[async_trait::async_trait]
//! # impl AsyncTestContext for MyAsyncContext {
//! # async fn setup() -> MyAsyncContext {
//! # MyAsyncContext { value: "Hello, world!".to_string() }
//! # }
//! # async fn teardown(self) {
//! # // Perform any teardown you wish.
//! # }
//! # }
//! #[test_context(MyAsyncContext)]
//! #[tokio::test]
//! async fn test_works(ctx: &mut MyAsyncContext) {
//! async fn test_async_works(ctx: &mut MyAsyncContext) {
//! assert_eq!(ctx.value, "Hello, World!");
//! }
//! ```