Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Defining Mocks

Mocks are defined in Rust using a simple, ergonomic builder-like API.

You can define your mocks first, then create a mock server with your mock set:

    // Create a mock set
    let mut mocks = MockSet::new();
    // Build and insert a mock
    mocks.mock(|when, then| {
        when.get().path("/health");
        then.text("healthy!");
    });
    // Alternatively, Mock::new() and mocks.insert(mock)

    // Create mock server with the mock set
    let mut server = MockServer::new_http("example").with_mocks(mocks);
    server.run().await?;

Or, you can create a mock server with a default empty mock set and register mocks directly to the server:

    // Create mock server
    let mut server = MockServer::new_http("example");
    server.run().await?;

    // Build and insert a mock to the server's mock set
    server.mock(|when, then| {
        when.get().path("/health");
        then.text("healthy!");
    });
    // Alternatively, use Mock::new() and server.mocks.insert(mock)