lets say I have a state and a command like this
#[derive(Default)]
pub struct MyState {
pub s: std::sync::Mutex<String>,
pub t: std::sync::Mutex<std::collections::HashMap<String, String>>,
}
#[tauri::command]
fn hello_world(state: tauri::State<'_, MyState>) -> String {
let mut s = state.s.lock().unwrap();
s.push_str(" world");
s.clone()
}
How do I access MyState
when I want to write a test for hello_world
function?
I haven't attempted any specific actions yet as I'm currently exploring how to approach testing for the hello_world
function in Tauri
. I'm seeking guidance on the correct methodology to access and manipulate the MyState
struct within the testing environment. Could you provide assistance on how to effectively set up and conduct tests for Tauri
commands involving state management?
I have struggled with this myself. There is a "hidden" unstable feature called
tauri::test
, which, as far as I can tell, is virtually undocumented in the user docs, but it's documented in the reference on docs.rs.You can see some examples here: docs.rs documentation, a snippet from the examples.
Because I also need it, I set up a small application with a working test here. Shortly:
Add the
test
feature in yourCargo.toml
:You can now use
tauri::test
for setting up your mock application:If you want to test on my repo, simply install the app as usual and then run the tests:
Remember that you won't test that the serialization is done correctly from the javascript side. For that, you will need end-to-end tests, which, fortunately, are in the user documentation.
Please let me know if this is enough for you. I know I didn't add a
State
example per-se, I can add aState
example as well.