-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.rs
39 lines (33 loc) · 797 Bytes
/
env.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use mockable::{DefaultEnv, Env};
#[derive(Clone, Debug, Eq, PartialEq)]
struct Config {
secret: String,
}
fn load(env: &dyn Env) -> Config {
Config {
secret: env.string("SECRET").expect("SECRET is not set"),
}
}
fn main() {
let cfg = load(&DefaultEnv);
println!("{cfg:?}");
}
#[cfg(test)]
mod test {
use mockable::MockEnv;
use mockall::predicate::eq;
use super::*;
#[test]
fn test() {
let expected = Config {
secret: "secret".into(),
};
let mut env = MockEnv::new();
env.expect_string().with(eq("SECRET")).returning({
let expected = expected.clone();
move |_| Some(expected.secret.clone())
});
let cfg = load(&env);
assert_eq!(cfg, expected);
}
}