Test Rust code without test-only traits or changes to production code. - XTSoftwareLabs/shimforge
The approach in this crate is you don't change any thing in your production code but directly mock functions such as std, reqwest and etc.
// Production code use std::fs;
pub fn claim_slot() -> Result<(), String> { fs::create_dir_all("/var/run/dispatcher") .map_err(|error| format!("cannot claim a slot: {error}")) }
// Test code
use shimforge::{Session, mock}; use std::io;
#[test] fn a_denied_slot_is_reported() { let mut session = Session::new(); let mkdir = mock!(session, fs::create_dir_all::<&str>, fn(&str) -> io::Result<()>); mkdir .expect() .with(|path| *path == "/var/run/dispatcher") .once() .returning(|_| Err(io::ErrorKind::PermissionDenied.into()));
let error = claim_slot().unwrap_err();
assert!(error.starts_with("cannot claim a slot"));
}0 comments
No comments yet.
Related stories
- Hacker News · 421 points · 6 days ago
- Hacker News · 1 points · 2 days ago
- Hacker News · 3 points · 3 days ago
- Hacker News · 1 points · 4 days ago
- Hacker News · 1 points · 6 days ago
- Hacker News · 2 points · about 12 hours ago