You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am attempting to write tests for my command code, based on the example in the docs. However, when attempting to pass an AppHandle to the command function, I get an error "the trait bound AppHandle: CommandArg<'_, R> is not satisfied".
Reproduction
Slightly modified from the docs:
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
// Note accessing the AppHandle here!
// Adding this is what's causing the problem.
#[tauri::command]
fn greet(_app: tauri::AppHandle, name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
fn create_app<R: tauri::Runtime>(mut builder: tauri::Builder<R>) -> tauri::App<R> {
builder
.setup(|app| {
// do something
Ok(())
})
.invoke_handler(tauri::generate_handler![greet])
// remove the string argument on your app
.build(tauri::generate_context!())
.expect("failed to build app")
}
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.unwrap();
}
#[cfg(test)]
mod tests {
#[test]
fn something() {
let data = r#"{"name": "the test"}"#;
let app = super::create_app(tauri::test::mock_builder());
let window = tauri::WebviewWindowBuilder::new(&app, "main", Default::default())
.build()
.unwrap();
// do something with the app and window
// in this case we'll run the my_cmd command with no arguments
tauri::test::assert_ipc_response(
&window,
tauri::webview::InvokeRequest {
cmd: "greet".into(),
callback: tauri::ipc::CallbackFn(0),
error: tauri::ipc::CallbackFn(1),
url: "http://tauri.localhost".parse().unwrap(),
body: tauri::ipc::InvokeBody::default(),
headers: Default::default(),
invoke_key: tauri::test::INVOKE_KEY.to_string(),
},
Ok("Hello, the test! You've been greeted from Rust!"),
);
}
}
error[E0277]: the trait bound `AppHandle: CommandArg<'_, R>` is not satisfied
--> src/welcome/mod.rs:20:1
|
20 | #[tauri::command]
| ^^^^^^^^^^^^^^^^^ the trait `serde::Deserialize<'_>` is not implemented for `AppHandle`, which is required by `AppHandle: CommandArg<'_, R>`
|
::: src/startup.rs:9:25
|
9 | .invoke_handler(tauri::generate_handler![
| _________________________-
10 | | welcome::get_saw_welcome_screen,
11 | | welcome::set_saw_welcome_screen
12 | | ])
| |_________- in this macro invocation
|
= help: the following other types implement trait `serde::Deserialize<'de>`:
`&'a [u8]` implements `serde::Deserialize<'de>`
`&'a serde_json::value::RawValue` implements `serde::Deserialize<'de>`
`&'a std::path::Path` implements `serde::Deserialize<'de>`
`&'a str` implements `serde::Deserialize<'de>`
`&'p jsonptr::pointer::Pointer` implements `serde::Deserialize<'de>`
`()` implements `serde::Deserialize<'de>`
`(T,)` implements `serde::Deserialize<'de>`
`(T0, T1)` implements `serde::Deserialize<'de>`
and 352 others
= note: required for `AppHandle` to implement `CommandArg<'_, R>`
= note: this error originates in the macro `welcome::__cmd__set_saw_welcome_screen` which comes from the expansion of the macro `tauri::generate_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
Additional context
No response
The text was updated successfully, but these errors were encountered:
mAAdhaTTah
changed the title
[bug] Testing docs example doesn't when command accepts AppHandle
[bug] Testing docs example doesn't compile when command accepts AppHandle
Dec 31, 2024
Describe the bug
I am attempting to write tests for my command code, based on the example in the docs. However, when attempting to pass an AppHandle to the command function, I get an error "the trait bound
AppHandle: CommandArg<'_, R>
is not satisfied".Reproduction
Slightly modified from the docs:
Expected behavior
Code should compile and successfully run tests.
Full
tauri info
outputStack trace
Additional context
No response
The text was updated successfully, but these errors were encountered: