#Mock Builder does not Call Setup

6 messages · Page 1 of 1 (latest)

indigo isle
#

I have a setup callback that registers in AppConfig struct via app.manage. This happens in the .setup(mysetupfunc) code. When I try to use this with the mock_builder, it says state() called before manage() for given type:
Here's my mock code:

 let app = mock_builder()
      .setup(|app| Ok(setup::setup(app, log_handles)?))
      .invoke_handler(handlers::generate())
      .build(tauri::generate_context!("../shell/tauri.conf.json"))
      .unwrap();

And then code like this:

    #[tokio::test]
    async fn it_setups() {
        let (app, app_data_dir) = setup_test_app().await;
        let config = app.state::<AppConfig>();

The issue is my setup callback is never being called. Is there guidance on how to call the setup manually? I believe it's called via .run in my main app. What's the protocol for calling it under tests? Do I just have to do everything my setup does manually?

indigo isle
#

Seems like the issue may have been the cli plugin. I added a cfg to conditionally load it. I had some logic that assumed the cli plugin was present that I thought i didn't have. I ended up just calling setup directly:

    async fn create_test_app() -> tauri::App<MockRuntime> {
        std::env::set_var("APP_ENV", "test");
        std::env::set_var("DATABASE_PATH", "test.db");
        std::env::set_var("SKIP_DOTENV", "true");
        let mut builder = mock_builder();
        builder = plugins::setup(builder);
        builder
            .setup(|_app| Ok(()))
            .invoke_handler(handlers::generate())
            .build(mock_context(noop_assets()))
            .unwrap()
    }

    #[tokio::test]
    async fn it_setups() {
        let log_handles = logging::setup().unwrap();
        let mut app = create_test_app().await;
        setup::setup_async(&mut app, log_handles).await.unwrap();
        let config = app.state::<AppConfig>();
        assert!(config.db_path.exists());
        assert!(config.logs_dir.exists());
        assert!(config.db_path.exists());
        let connection = get_dbc(&config.db_path).await.unwrap();
        let query = sea_orm::Statement::from_string(
            connection.get_database_backend(),
            "SELECT COUNT(*) as count FROM seaql_migrations",
        );
        let result: QueryResult = connection.query_one(query).await.unwrap().unwrap();
        let count: i32 = result.try_get("", "count").unwrap();
        assert_eq!(count, migration::Migrator::migrations().len() as i32);
    }
}
#

Here's the logic I had to add to Config struct to avoid the use of it during tests:

    #[cfg(any(target_os = "ios", target_os = "android", test))]
    pub fn matches<R: Runtime>(_app: &App<R>) -> HashMap<String, Value> {
        HashMap::new()
    }
    #[cfg(not(any(target_os = "ios", target_os = "android", test)))]
    pub fn matches<R: Runtime>(app: &App<R>) -> HashMap<String, Value> {
        app.cli()
            .matches()
            .unwrap()
            .args
            .iter()
            .map(|(key, arg_data)| (key.clone(), arg_data.value.clone()))
            .collect()
    }
indigo isle
#

So I was able to get the Cli Plugin working with tests. Here's what I had to do. You might not have to do this if you're just generating the default context.

#[cfg(test)]
pub mod testutils {
    use super::{handlers, plugins};

    // ... imports
    pub fn create_context<R: Runtime>() -> Context<R> {
        let mut plugins_config = HashMap::new();
        let cli_config = json!({
            "description": "Test CLI",
            "longDescription": "Test CLI for unit tests",
            "beforeHelp": "",
            "afterHelp": "",
            "args": [],
            "subcommands": {}
        });
        plugins_config.insert("cli".to_string(), cli_config);
        let mut context = mock_context(noop_assets());
        let config = context.config_mut();
        config.plugins = PluginConfig(plugins_config);
        context
    }

    pub fn create_app() -> Result<App<MockRuntime>> {
        std::env::set_var("APP_ENV", "test");
        std::env::set_var("DATABASE_PATH", "test.db");
        std::env::set_var("NO_DOTENV", "true");
        let mut builder = mock_builder();
        builder = plugins::load(builder);
        Ok(builder
            .setup(|_app| Ok(()))
            .invoke_handler(handlers::generate())
            .build(create_context())?)
    }
}
visual oracle
#

@indigo isle Thank you! That's the information i was looking for 🙂

indigo isle