mt-mock-logger

Crates.iomt-mock-logger
lib.rsmt-mock-logger
version0.1.2
sourcesrc
created_at2024-09-23 19:00:03.195958
updated_at2024-09-24 00:59:40.25471
descriptionA thread-safe mock logger for multiple tests running in parallel
homepage
repositoryhttps://github.com/diogomattioli/mt_mock_logger
max_upload_size
id1384374
size7,698
Diogo Mattioli (diogomattioli)

documentation

README

mt-mock-logger

A thread-safe mock logger designed to support mocking across multiple tests running in parallel.

The injected logger is automatically scoped to the lifetime of the returned MockLoggerGuard provided by the set_logger function.

The MockLogger is not intended for use with async. Since it binds the injected logger to the test's ThreadId, using it in an async context may result in unexpected behavior.

Usage

[dev-dependencies]
mt-mock-logger = "0.1"
#[cfg(test)]
mod tests {
    use mt_mock_logger::MockLogger;
    use mockall::mock;

    use super::*;

    mock! {
        pub MyLogger {}
        impl log::Log for MyLogger {
            fn enabled<'a>(&self, metadata: &log::Metadata<'a>) -> bool;
            fn log<'a>(&self, record: &log::Record<'a>);
            fn flush(&self);
        }
    }

    #[test]
    fn test_logging() {
        let mut my_logger = MockMyLogger::new();
        my_logger
            .expect_log()
            .withf(|r| r.level() == log::LevelFilter::Info && 
                        r.args().as_str() == Some("ok"))
            .once()
            .return_const(());

        let _guard = MockLogger::set_logger(my_logger, log::LevelFilter::Info);

        log::info!("ok");
        log::trace!("ok");
    }

    #[test]
    fn test_logging_below_max_level() {
        let mut my_logger = MockMyLogger::new();
        my_logger.expect_log().never().return_const(());

        let _guard = MockLogger::set_logger(my_logger, log::LevelFilter::Info);

        log::trace!("ok");
    }

    #[test]
    fn test_no_logger() {
        log::trace!("ok");
    }
}
Commit count: 23

cargo fmt