pub struct Builder<R, T, RequestState> { /* private fields */ }
Expand description
Builder for creating crate::mock::Mock
services and testing them with a
tower_async_layer::Layer
.
This generic builder is designed to make it easy to test your own
tower_async_layer::Layer
in a type-safe and guided manner. It achieves
this by using the “Builder” pattern and a secure state flow guided by the Rust
type system.
§Flow
define another cycle
┌───────┐
│ │
│ │
┌───────────────┐ ┌───────▼───────┴───────┐ ┌─────────────────────────┐
│Define │ │Define <Response> │ │Execute and await the │
│<Input> Request├─────►or <Error> to send from├──────────────►test using the previously│
└───────────────┘ │within the Mock Service│ │defined flow and using │
│to your Layer. │ ┌──────────►the passed in Layer to │
└──────▲─────┬──────────┘ │ │generate the service used│
│ │ │ │for testing. │
define │ │ │ └─────────────┬───────────┘
another │ │ │ │
cycle │ │ │ │
┌─────┴─────▼──────────────┴─┐ ┌─────────────▼─────────────┐
│Define the expected │ │Optionally expect the final│
│<Request> received by │ │Output or Error (Result). │
│the Mock Service (Optional).│ │ │
└────────────────────────────┘ └───────────────────────────┘
§Examples
use tower_async_test::Builder;
use tower_async_layer::Identity;
#[tokio::main]
async fn main() {
Builder::new("ping")
.send_response("pong")
.expect_request("ping")
.test(Identity::new())
.await
.expect_response("pong");
}
Implementations§
Source§impl<R> Builder<R, None, None>
Virgin Builder
impl<R> Builder<R, None, None>
Virgin Builder
Source§impl<R, Response, RequestState> Builder<R, Vec<Test<R, Ok<Response>>>, RequestState>
Ok-only test builder
impl<R, Response, RequestState> Builder<R, Vec<Test<R, Ok<Response>>>, RequestState>
Ok-only test builder
Source§impl<R, Response, RequestState> Builder<R, Vec<Test<R, Ok<Response>>>, RequestState>
impl<R, Response, RequestState> Builder<R, Vec<Test<R, Ok<Response>>>, RequestState>
Sourcepub async fn test<L>(
self,
layer: L,
) -> ResponseTester<<<L as Layer<Mock<R, Response, Infallible>>>::Service as Service<R>>::Response, <<L as Layer<Mock<R, Response, Infallible>>>::Service as Service<R>>::Error>
pub async fn test<L>( self, layer: L, ) -> ResponseTester<<<L as Layer<Mock<R, Response, Infallible>>>::Service as Service<R>>::Response, <<L as Layer<Mock<R, Response, Infallible>>>::Service as Service<R>>::Error>
Test the given tower_async_layer::Layer
with the previously registered tests.
§Panics
Panics if there are less requests returned then there are responses+errors registered.
Source§impl<R, Error, RequestState> Builder<R, Vec<Test<R, Err<Error>>>, RequestState>
Error-only test builder
impl<R, Error, RequestState> Builder<R, Vec<Test<R, Err<Error>>>, RequestState>
Error-only test builder
Source§impl<R, Error, RequestState> Builder<R, Vec<Test<R, Err<Error>>>, RequestState>
impl<R, Error, RequestState> Builder<R, Vec<Test<R, Err<Error>>>, RequestState>
Sourcepub async fn test<L>(
self,
layer: L,
) -> ResponseTester<<<L as Layer<Mock<R, (), Error>>>::Service as Service<R>>::Response, <<L as Layer<Mock<R, (), Error>>>::Service as Service<R>>::Error>
pub async fn test<L>( self, layer: L, ) -> ResponseTester<<<L as Layer<Mock<R, (), Error>>>::Service as Service<R>>::Response, <<L as Layer<Mock<R, (), Error>>>::Service as Service<R>>::Error>
Test the given layer with the previously registered tests.
§Panics
Panics if there are less requests returned then there are responses+errors registered.
Source§impl<R, Response, Error, RequestState> Builder<R, Vec<Test<R, Result<Response, Error>>>, RequestState>
Full Result (Ok+Err mix) test builder
impl<R, Response, Error, RequestState> Builder<R, Vec<Test<R, Result<Response, Error>>>, RequestState>
Full Result (Ok+Err mix) test builder
Source§impl<R, Response, Error, RequestState> Builder<R, Vec<Test<R, Result<Response, Error>>>, RequestState>
impl<R, Response, Error, RequestState> Builder<R, Vec<Test<R, Result<Response, Error>>>, RequestState>
Sourcepub async fn test<L>(
self,
layer: L,
) -> ResponseTester<<<L as Layer<Mock<R, Response, Error>>>::Service as Service<R>>::Response, <<L as Layer<Mock<R, Response, Error>>>::Service as Service<R>>::Error>
pub async fn test<L>( self, layer: L, ) -> ResponseTester<<<L as Layer<Mock<R, Response, Error>>>::Service as Service<R>>::Response, <<L as Layer<Mock<R, Response, Error>>>::Service as Service<R>>::Error>
Test the given layer with the previously registered tests.
§Panics
Panics if there are less requests returned then there are responses+errors registered.