cached icon indicating copy to clipboard operation
cached copied to clipboard

Disable caching for tests?

Open Clete2 opened this issue 3 years ago • 0 comments

Is there an easy way to disable caching for testing purposes?

A simple example is below; where I cache a URL. The second test always fails because the result is still cached from the first test. More complex examples also fail due to caching.

use cached::proc_macro::cached;

#[cached]
fn url() -> String {
    std::env::var("hostname").unwrap_or_else(|_| String::from("some.hostname"))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn url_default() {
        std::env::remove_var("hostname");
        assert_eq!(url(), "some.hostname");
    }

    #[test]
    fn url_override() {
        std::env::set_var("hostname", "some.other.hostname");
        assert_eq!(url(), "some.other.hostname");
    }
}

Clete2 avatar Sep 06 '22 13:09 Clete2