Test a command that uses state #11717
-
I have a command in Rust that looks like the following: pub struct AppContext {
pub response: String,
}
#[tauri::command]
pub fn ping(state: State<'a, AppContext>) -> Result<String, String> {
Ok(state.response)
} I'd like to write a unit test for this command. Something that looks like the following: #[cfg(test)]
mod test {
use super::*;
#[test]
fn test_pong() {
let context = AppContext { response: "pong".into() };
let state = State::new(context); // State's implementation in Tauri is private, so State::new() does not actually exist
let response = ping(state).unwrap();
assert_eq!(response, "pong".to_string());
}
} From what I can tell looking through Tauri's code is that So it seems like it's impossible to write a unit test for commands? FWIW, with the See also #8424 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
What about something like this? #[tauri::command]
fn something(state: tauri::State<String>) -> String {
state.inner().clone()
}
#[cfg(test)]
mod tests {
use super::something;
use tauri::Manager;
#[test]
pub fn test_something() {
let app = tauri::test::mock_app();
app.manage("something".to_string());
assert_eq!(&something(app.state::<String>()), "something");
}
} I'm no expert but something like |
Beta Was this translation helpful? Give feedback.
What about something like this?
I'm no expert but something like
State::new()
could be problematic because then we'd have to give it a 'static lifetime which doesn't match real behavior and would allow more things than non-static lifetimes. The lifetime in the approach i showed also behaves a bit differently i th…