Skip to content

Latest commit

 

History

History
64 lines (49 loc) · 1.61 KB

README.md

File metadata and controls

64 lines (49 loc) · 1.61 KB

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");
    }
}