Struct wiremock::MockGuard

source ·
pub struct MockGuard { /* private fields */ }
Expand description

You get a MockGuard when registering a scoped Mock using MockServer::register_as_scoped or Mock::mount_as_scoped.

When the MockGuard is dropped, the MockServer verifies that the expectations set on the scoped Mock were verified - if not, it will panic.

§Limitations

When expectations of a scoped Mock are not verified, it will trigger a panic - just like a normal Mock. Due to limitations in Rust’s Drop trait, the panic message will not include the filename and the line location where the corresponding MockGuard was dropped - it will point into wiremock’s source code.

This can be an issue when you are using more than one scoped Mock in a single test - which of them panicked? To improve your debugging experience it is strongly recommended to use Mock::named to assign a unique identifier to your scoped Mocks, which will in turn be referenced in the panic message if their expectations are not met.

Implementations§

source§

impl MockGuard

source

pub async fn received_requests(&self) -> Vec<Request>

Return all the requests that have been matched by the corresponding scoped Mock since it was mounted.
The requests are returned in the order they were received.

It returns an empty vector if no request has been matched.

source

pub async fn wait_until_satisfied(&self)

This method doesn’t return until the expectations set on the corresponding scoped Mock are satisfied.

It can be useful when you are testing asynchronous flows (e.g. a message queue consumer) and you don’t have a good event that can be used to trigger the verification of the expectations set on the scoped Mock.

§Timeouts

There is no default timeout for this method, so it will end up waiting forever if your expectations are never met. Probably not what you want.

It is strongly recommended that you set your own timeout using the appropriate timers from your chosen async runtime.
Since wiremock is runtime-agnostic, it cannot provide a default timeout mechanism that would work for all users.

use std::time::Duration;
use wiremock::{Mock, MockServer, ResponseTemplate};
use wiremock::matchers::method;

#[tokio::main]
async fn main() {
    // Arrange
    let mock_server = MockServer::start().await;

    let response = ResponseTemplate::new(200);
    let mock = Mock::given(method("GET")).respond_with(response);
    let mock_guard = mock_server.register_as_scoped(mock).await;
     
    // Act
    let waiter = mock_guard.wait_until_satisfied();
    // Here we wrap the waiter in a tokio timeout
    let outcome = tokio::time::timeout(Duration::from_millis(10), waiter).await;

    // Assert
    assert!(outcome.is_err());
}

Trait Implementations§

source§

impl Drop for MockGuard

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more