[][src]Struct httpmock::MockRef

pub struct MockRef<'a> { /* fields omitted */ }

Represents a reference to the mock object on a MockServer. It can be used to spy on the mock and also perform some management operations, such as deleting the mock from the MockServer.

Example

// Arrange: Create mock server and a mock
use httpmock::{MockServer, Mock};

let mock_server = MockServer::start();
let mut mock = Mock::new()
    .expect_path_contains("/test")
    .return_status(202)
    .create_on(&mock_server);

// Send a first request, then delete the mock from the mock and send another request.
let response1 = isahc::get(mock_server.url("/test")).unwrap();

// Fetch how often this mock has been called from the server until now
assert_eq!(mock.times_called(), 1);

// Delete the mock from the mock server
mock.delete();

let response2 = isahc::get(mock_server.url("/test")).unwrap();

// Assert that the mock worked for the first request, but not for the second request,
// because it was deleted before the second request was sent.
 assert_eq!(response1.status(), 202);
 assert_eq!(response2.status(), 404);

Implementations

impl<'a> MockRef<'a>[src]

pub fn times_called(&self) -> usize[src]

This method returns the number of times a mock has been called at the mock server.

Example

// Arrange: Create mock server and a mock
use httpmock::{MockServer, Mock};

let mock_server = MockServer::start();
let mut mock = Mock::new()
    .expect_path_contains("/times_called")
    .return_status(200)
    .create_on(&mock_server);

// Send a first request, then delete the mock from the mock and send another request.
let response1 = isahc::get(mock_server.url("/times_called")).unwrap();

// Fetch how often this mock has been called from the server until now
assert_eq!(mock.times_called(), 1);

Panics

This method will panic if there is a problem with the (remote) server.

pub async fn times_called_async<'_>(&'_ self) -> usize[src]

This method returns the number of times a mock has been called at the mock server. This method is the asynchronous equivalent of MockRef::times_called.

Example

use httpmock::{MockServer, Mock};
use tokio_test::block_on;

#[async_std::test]
async fn example_test() -> std::io::Result<()> {
    // Arrange
    let mock_server = MockServer::start_async().await;
    let mut mock = Mock::new()
        .expect_path_contains("/times_called")
        .return_status(200)
        .create_on_async(&mock_server)
        .await;

    // Act
    let response1 = isahc::get_async(mock_server.url("/times_called")).await.unwrap();

    // Assert
    assert_eq!(mock.times_called_async().await, 1);
}

Panics

This method will panic if there is a problem to communicate with the server.

pub fn delete(&mut self)[src]

Deletes this mock from the mock server.

Example

use httpmock::{MockServer, Mock};

// Arrange: Create mock server and a mock
let mock_server = MockServer::start();
let mut mock = Mock::new()
    .expect_path_contains("/test")
    .return_status(202)
    .create_on(&mock_server);

// Send a first request, then delete the mock from the mock and send another request.
let response1 = isahc::get(mock_server.url("/test")).unwrap();

// Delete the mock from the mock server
mock.delete();

let response2 = isahc::get(mock_server.url("/test")).unwrap();

// Assert that the mock worked for the first request, but not for the second request,
// because it was deleted before the second request was sent.
assert_eq!(response1.status(), 202);
assert_eq!(response2.status(), 404);

Panics

This method will panic if there is a problem to communicate with the server.

pub async fn delete_async<'_>(&'_ self)[src]

Deletes this mock from the mock server. This method is the asynchronous equivalent of MockRef::delete.

Example

// Arrange: Create mock server and a mock
use httpmock::{MockServer, Mock};

let mock_server = MockServer::start();
let mut mock = Mock::new()
    .expect_path_contains("/test")
    .return_status(202)
    .create_on(&mock_server);

// Send a first request, then delete the mock from the mock and send another request.
let response1 = isahc::get(mock_server.url("/test")).unwrap();

// Delete the mock from the mock server
mock.delete();

let response2 = isahc::get(mock_server.url("/test")).unwrap();

// Assert that the mock worked for the first request, but not for the second request,
// because it was deleted before the second request was sent.
assert_eq!(response1.status(), 202);
assert_eq!(response2.status(), 404);

Panics

This method will panic if there is a problem to communicate with the server.

pub fn server_address(&self) -> &SocketAddr[src]

Returns the address of the mock server this mock is using. By default this is "localhost:5000" if not set otherwise by the environment variables HTTPMOCK_HOST and HTTPMOCK_PORT.

Auto Trait Implementations

impl<'a> !RefUnwindSafe for MockRef<'a>

impl<'a> Send for MockRef<'a>

impl<'a> Sync for MockRef<'a>

impl<'a> Unpin for MockRef<'a>

impl<'a> !UnwindSafe for MockRef<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> WithSubscriber for T[src]