Trait httpmock::MockExt [−][src]
pub trait MockExt<'a> {
fn new(id: usize, mock_server: &'a MockServer) -> Mock<'a>;
fn id(&self) -> usize;
}
Expand description
Required methods
fn new(id: usize, mock_server: &'a MockServer) -> Mock<'a>
fn new(id: usize, mock_server: &'a MockServer) -> Mock<'a>
Creates a new Mock instance that references an already existing mock on a MockServer. This functionality is usually not required. You can use it if for you need to recreate Mock instances .
id
- The ID of the existing mock ot the MockServer.mock_server
- The MockServer to which the Mock instance will reference.
Example
use httpmock::{MockServer, Mock, MockExt};
use isahc::get;
// Arrange
let server = MockServer::start();
let mock_ref = server.mock(|when, then| {
when.path("/test");
then.status(202);
});
// Store away the mock ID for later usage and drop the Mock instance.
let mock_id = mock_ref.id();
drop(mock_ref);
// Act: Send the HTTP request
let response = get(server.url("/test")).unwrap();
// Create a new Mock instance that references the earlier mock at the MockServer.
let mock_ref = Mock::new(mock_id, &server);
// Use the recreated Mock as usual.
mock_ref.assert();
assert_eq!(response.status(), 202);
Refer to [Issue 26
][https://github.com/alexliesenfeld/httpmock/issues/26] for more
information.
Returns the ID that the mock was assigned to on the MockServer.