Skip to content

Test a command that uses state #11717

Answered by FabianLars
Nick-Mazuk asked this question in Q&A
Discussion options

You must be logged in to vote

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 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…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Nick-Mazuk
Comment options

Answer selected by Nick-Mazuk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants