pub trait ExpectOp:
ExpectOpExt
+ Debug
+ Send
+ 'static {
// Provided methods
fn on_any(
&self,
context: &mut Context<'_>,
received: &Value,
) -> ExpectOpResult<()> { ... }
fn on_null(&self, context: &mut Context<'_>) -> ExpectOpResult<()> { ... }
fn on_f64(
&self,
context: &mut Context<'_>,
received: f64,
) -> ExpectOpResult<()> { ... }
fn on_u64(
&self,
context: &mut Context<'_>,
received: u64,
) -> ExpectOpResult<()> { ... }
fn on_i64(
&self,
context: &mut Context<'_>,
received: i64,
) -> ExpectOpResult<()> { ... }
fn on_boolean(
&self,
context: &mut Context<'_>,
received: bool,
) -> ExpectOpResult<()> { ... }
fn on_string(
&self,
context: &mut Context<'_>,
received: &str,
) -> ExpectOpResult<()> { ... }
fn on_array(
&self,
context: &mut Context<'_>,
received: &[Value],
) -> ExpectOpResult<()> { ... }
fn on_object(
&self,
context: &mut Context<'_>,
received: &Map<String, Value>,
) -> ExpectOpResult<()> { ... }
fn debug_supported_types(&self) -> &'static [JsonType] { ... }
}Expand description
The trait that represents an expectation. It needs to be used in
conjunction with the super::expect_op macro.
§Example
Here is an example checking if the value returned is a string, and of a minimum length, using Axum Test.
use axum_test::expect_json;
use axum_test::expect_json::expect_core::ExpectOp;
use axum_test::expect_json::expect_core::ExpectOpResult;
use axum_test::expect_json::expect_core::expect_op;
use axum_test::expect_json::expect_core::Context;
// 1. Implement a struct representing your expectation.
// This needs to include the `expect_op`, and the contents must be serializable.
#[expect_op]
#[derive(Clone, Debug)]
struct ExpectStrMinLen {
min: usize,
}
// 2. Implement `ExpectOp`, and implement the types you want to check for. Here we check against strings.
impl ExpectOp for ExpectStrMinLen {
fn on_string(&self, _context: &mut Context<'_>, received: &str) -> ExpectOpResult<()> {
if received.len() < self.min {
panic!("String is too short, received: {received}");
}
Ok(())
}
}
// 3. Build a router to test against.
let app = Router::new().route(&"/user", get(|| async {
Json(json!({
"name": "Joe",
"age": 20,
}))
}));
let server = TestServer::new(app).unwrap();
// 4. Use the new expectation!
server.get(&"/user").await.assert_json(&json!({
"name": ExpectStrMinLen { min: 3 },
"age": 20,
}));Provided Methods§
fn on_any( &self, context: &mut Context<'_>, received: &Value, ) -> ExpectOpResult<()>
fn on_null(&self, context: &mut Context<'_>) -> ExpectOpResult<()>
fn on_f64(&self, context: &mut Context<'_>, received: f64) -> ExpectOpResult<()>
fn on_u64(&self, context: &mut Context<'_>, received: u64) -> ExpectOpResult<()>
fn on_i64(&self, context: &mut Context<'_>, received: i64) -> ExpectOpResult<()>
fn on_boolean( &self, context: &mut Context<'_>, received: bool, ) -> ExpectOpResult<()>
fn on_string( &self, context: &mut Context<'_>, received: &str, ) -> ExpectOpResult<()>
fn on_array( &self, context: &mut Context<'_>, received: &[Value], ) -> ExpectOpResult<()>
fn on_object( &self, context: &mut Context<'_>, received: &Map<String, Value>, ) -> ExpectOpResult<()>
Sourcefn debug_supported_types(&self) -> &'static [JsonType]
fn debug_supported_types(&self) -> &'static [JsonType]
This is optional to implement. This method returns a list of types this is targeting.
This is used for debug messages for the user, when the type doesn’t match up.