#[mock]
Expand description
Starts a Stubr mock server and creates a stubr
variable which can be used to call the server e.g. stubr.uri()
.
It supports both standard and async test functions.
Example
use asserhttp::*; // optional
#[test]
#[stubr::mock] // <- takes stubs under crate's "tests/stubs" by default
#[should_panic] // <- if required place it after '#[stubr::mock]' to avoid unused warnings
fn simple_test() {
isahc::get(stubr.uri()).expect_status_ok();
}
// also works for async functions
#[async_std::test]
#[stubr::mock]
async fn async_test() {
isahc::get(stubr.uri()).await.expect_status_ok();
}
Configuration
// path to stub file (or directory) under crate's "tests/stubs"
#[test]
#[stubr::mock("path/to/stubs")] // <- or "path/to/stubs/stub.json" for a single file
fn default() {}
// absolute path ; not appending to "tests/stubs"
#[test]
#[stubr::mock(full_path = "tests/other/stubs")]
fn full_path() {}
// start on a dedicated port
#[test]
#[stubr::mock(port = 1234)]
fn port() {}