Closed SvenKeimpema closed 7 months ago
state() called before manage() for given type
This error is kind of self-explanatory, you're trying to retrieve a state using .state()
before storing it using .manage()
. You don't need a command to store a state, you can just do app.manage(YourState)
@amrbashir Hey, sorry forgot to add the code how i create the app:
pub fn create_app<R: tauri::Runtime>(builder: tauri::Builder<R>) -> tauri::App<R> {
let app_state = TauriStateHolder(
Mutex::new(Game { ..Default::default() }),
);
// init the game so we can make moves, see the board, ect.
app_state.0.lock().unwrap().init_game();
builder
.manage(app_state)
.invoke_handler(tauri::generate_handler![
get_board, select_square, move_piece, undo_move, check_game_won
])
.build(tauri::generate_context!())
.expect("failed to build app")
}
In here i do call manage however it seems to not accessible/forgotten whenever i use create_app in a test:
#[test]
fn test_make_move() {
let app: App<MockRuntime> = create_app(tauri::test::mock_builder());
let window: Window<MockRuntime> = app.get_window("main").unwrap();
call_make_move(&window, r#"{"startSq": 52, "destSq": 36}"#);
//error: `state() called before manage() for given type`
let game_state = window.state::<ChessGameState>();
println!("{:?}", get_bitboard(&window).unwrap());
}
You called manage on TauriStateHelper
type but you're trying to retreive ChessGameState
which is not the same type you stored and that's why it is failing.
for anyone who has this issue in the future instead of getting the state which you stored, you need to get the tauri state:
let app: App<MockRuntime> = create_app(tauri::test::mock_builder());
let window: Window<MockRuntime> = app.get_window("main").unwrap();
window.state::<TauriStateHolder>()
Describe the bug
I'm currently trying to add unit tests to my tauri application with tauri's tauri::test::mock_builder(). However i am currently having some issues cloning/accessing the gamestate.
The reason why i don't to give out my game information with tauri's commands(tauri::command) is due to it being pretty usafe since end-users can also call these command's with js(in the browser console).
I've also tried to get the state by calling
window.state
:However this give me the error:
Is there a possible way around this without creating commands to do this? Or is it simply not implemented yet?
Reproduction
No response
Expected behavior
No response
Full
tauri info
outputStack trace
Additional context
No response