Skip to content

Update Rust crate mockito to v1

Óscar García Amor requested to merge renovate/mockito-1.x into master

This MR contains the following updates:

Package Type Update Change
mockito dev-dependencies major ~0.32 -> ~1.0.0

Release Notes

lipanski/mockito

v1.0.0

Compare Source

🎈 7 years and 63 releases later, it's finally time for the 1.0 🎈

Changes

  • [Breaking] The legacy interface was removed in this version
  • [Breaking] Mock::with_body_from_fn was renamed to Mock::with_chunked_body - the former is still supported with a deprecation warning
  • Mocks are only cleared when the server is dropped, not when the mock is dropped - this means you don't have to assign mocks to variables any more (unless you want to call other methods on them)
  • Introduced the Mock::remove and Mock::remove_async methods to remove mocks on demand

Major changes since 0.31

  • Tests can now run in parallel
  • Support for HTTP2
  • An async interface for all actions (though the sync interface is also available)
  • Mock multiple server/hosts at the same time

For a list of all the changes please check the release log.

Migrating to the new API

Legacy API:

let m1 = mockito::mock("GET", "/hello").with_body("hello").create();
let m2 = mockito::mock("GET", "/bye").with_body("bye").create();

// Use one of these to configure your client
let host = mockito:server_address();
let url = mockito::server_url();

New API:

let mut server = mockito::Server::new();
server.mock("GET", "/hello").with_body("hello").create();
server.mock("GET", "/bye").with_body("bye").create();

// Use one of these to configure your client
let host = server.host_with_port();
let url = server.url();

If you can't migrate to the new API in one go, consider using version 0.32.5, which supports both the legacy API as well as the new API.

Migrating to the async API

In order to write async tests, you'll need to use the _async methods:

  • Server::new_async
  • Mock::create_async
  • Mock::assert_async
  • Mock::matched_async
  • Mock::remove_async
  • Server::reset_async

...otherwise your tests will not compile and you'll see this error:

Cannot block the current thread from within a runtime. This happens because a function attempted 
to block the current thread while the thread is being used to drive asynchronous tasks.

Example:


#[tokio::test]
async fn test_simple_route_mock_async() {
    let mut server = Server::new_async().await;
    let m1 = server.mock("GET", "/a").with_body("aaa").create_async().await;
    let m2 = server.mock("GET", "/b").with_body("bbb").create_async().await;

    let (m1, m2) = futures::join!(m1, m2);

    // You can use `Mock::assert_async` to verify that your mock was called
    // m1.assert_async().await;
    // m2.assert_async().await;
}

v0.32.5

Compare Source

  • Implement and enable a new server pool and get rid of the deadpool dependency
  • Replace the mpsc mock/server communication with Arc (more reliable in this case)
  • Split sync & async paths more clearly

v0.32.4

Compare Source

  • Introduce Mock::with_body_from_request which allows setting the response body dynamically, based on the Request object
  • Small performance improvement: replace the state Mutex with an RwLock
  • Small fixes to the documentation (thanks @​konstin)

v0.32.3

Compare Source

  • Various fixes addressing hanging/flickering tests
  • Server::new and Server::new_async now return a ServerGuard object which dereferences to Server - this is only relevant if you need to assign a type to your server
  • The server pool has been disabled and will be brought back in a future release

v0.32.2

Compare Source

  • Prevent the Mock destructor from hanging in some cases
  • Updates to the docs, clarifying the usage of _async methods

v0.32.1

Compare Source


Configuration

📅 Schedule: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

Rebasing: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 Ignore: Close this MR and you won't be reminded about this update again.


  • If you want to rebase/retry this MR, check this box

This MR has been generated by Renovate Bot.

Merge request reports