[−][src]Struct wiremock::Mock
Given a set of matchers, a Mock
instructs an instance of MockServer
to return a pre-determined response if the matching conditions are satisfied.
Mock
s have to be mounted (or registered) with a MockServer
to become effective.
Example (using register
):
use wiremock::{MockServer, Mock, ResponseTemplate}; use wiremock::matchers::method; #[async_std::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.clone()); // Registering the mock with the mock server - it's now effective! mock_server.register(mock).await; // We won't register this mock instead. let unregistered_mock = Mock::given(method("GET")).respond_with(response); // Act let status = surf::get(&mock_server.uri()) .await .unwrap() .status(); assert_eq!(status.as_u16(), 200); // This would have matched `unregistered_mock`, but we haven't registered it! // Hence it returns a 404, the default response when no mocks matched on the mock server. let status = surf::post(&mock_server.uri()) .await .unwrap() .status(); assert_eq!(status.as_u16(), 404); }
Example (using mount
):
If you prefer a fluent style, you can use the mount
method on the Mock
itself
instead of register
.
use wiremock::{MockServer, Mock, ResponseTemplate}; use wiremock::matchers::method; #[async_std::main] async fn main() { // Arrange let mock_server = MockServer::start().await; Mock::given(method("GET")) .respond_with(ResponseTemplate::new(200)) // Mounting the mock on the mock server - it's now effective! .mount(&mock_server) .await; // Act let status = surf::get(&mock_server.uri()) .await .unwrap() .status(); assert_eq!(status.as_u16(), 200); }
Both register
and mount
are asynchronous methods - don't forget to .await
them!
Methods
impl Mock
[src]
pub fn given<M: 'static + Match>(matcher: M) -> MockBuilder
[src]
Start building a Mock
specifying the first matcher.
It returns an instance of MockBuilder
.
pub async fn mount<'_>(self, server: &'_ MockServer)
[src]
Mount a Mock
on an instance of MockServer
.
Be careful! Mock
s are not effective until they are mount
ed or register
ed on a MockServer
.
mount
is an asynchronous method, make sure to .await
it!
pub fn response(&self) -> Response
[src]
Build an instance of http_types::Response
from the ResponseTemplate
associated
with a Mock
.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Mock
impl Send for Mock
impl Sync for Mock
impl Unpin for Mock
impl !UnwindSafe for Mock
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> AsAny for T where
T: Any,
T: Any,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Message for T where
T: Any + Send + Sync + Debug,
[src]
T: Any + Send + Sync + Debug,
impl<T> State for T where
T: Send + Sync + 'static,
T: Send + Sync + 'static,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,