Test Rust code without test-only traits or changes to production code. - XTSoftwareLabs/shimforge

1 points•xtsoftwarelabs•7 days ago•0 comments•
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