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 Mock
s, which will in turn be referenced in the panic message if their expectations are
not met.
Implementations§
source§impl MockGuard
impl MockGuard
sourcepub async fn received_requests(&self) -> Vec<Request>
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.
sourcepub async fn wait_until_satisfied(&self)
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());
}