Available on crate feature
test only.Expand description
Utilities for unit testing on Tauri applications.
§Stability
This module is unstable.
§Examples
use tauri::test::{mock_builder, mock_context, noop_assets};
#[tauri::command]
fn ping() -> &'static str {
"pong"
}
fn create_app<R: tauri::Runtime>(builder: tauri::Builder<R>) -> tauri::App<R> {
builder
.invoke_handler(tauri::generate_handler![ping])
// remove the string argument to use your app's config file
.build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
.expect("failed to build app")
}
fn main() {
// Use `tauri::Builder::default()` to use the default runtime rather than the `MockRuntime`;
// let app = create_app(tauri::Builder::default());
let app = create_app(mock_builder());
let webview = tauri::WebviewWindowBuilder::new(&app, "main", Default::default()).build().unwrap();
// run the `ping` command and assert it returns `pong`
let res = tauri::test::get_ipc_response(
&webview,
tauri::webview::InvokeRequest {
cmd: "ping".into(),
callback: tauri::ipc::CallbackFn(0),
error: tauri::ipc::CallbackFn(1),
// alternatively use "tauri://localhost"
url: "http://tauri.localhost".parse().unwrap(),
body: tauri::ipc::InvokeBody::default(),
headers: Default::default(),
invoke_key: tauri::test::INVOKE_KEY.to_string(),
},
).map(|b| b.deserialize::<String>().unwrap());
}Structs§
- Event
Proxy - Mock
Runtime - Mock
Runtime Handle - Mock
Webview Dispatcher - Mock
Window Builder - Mock
Window Dispatcher - Noop
Asset - An empty
Assetsimplementation. - Runtime
Context
Constants§
- INVOKE_
KEY - The invoke key used for tests.
Functions§
- assert_
ipc_ response - Executes the given IPC message and assert the response matches the expected value.
- get_
ipc_ response - Executes the given IPC message and get the return value.
- mock_
app - Creates a new
Appfor testing using themock_contextwith anoop_assets. - mock_
builder - Creates a new
Builderusing theMockRuntime. - mock_
context - Creates a new
crate::Contextfor testing. - noop_
assets - Creates a new empty
Assetsimplementation.