cached
cached copied to clipboard
Disable caching for tests?
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");
}
}