Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: A new mocking library to mock functions without using trait
Our team decided to open source this as we think it could benefit the whole rust community. Also we are seeking feedback from the community to make it better: injectorpp
In short, injectorpp
allows you to mock functions without using trait.
For example, to write tests for below code:
fn try_repair() -> Result<(), String> {
if let Err(e) = fs::create_dir_all("/tmp/target_files") {
// Failure business logic here
return Err(format!("Could not create directory: {}", e));
}
// Success business logic here
Ok(())
}
You don't need trait. Below code just works
let mut injector = InjectorPP::new();
injector
.when_called(injectorpp::func!(fs::create_dir_all::<&str>))
.will_execute(injectorpp::fake!(
func_type: fn(path: &str) -> std::io::Result<()>,
when: path == "/tmp/target_files",
returns: Ok(()),
times: 1
));
assert!(try_repair().is_ok());
In my current career as a software developer, I saw most if not all the articles are asking developers to use interfaces to make the code unit testable. After seeing many "interfaces" or "traits" that are used solely for testing purpose and made the project complex, I start to doubt it. Is the test only interface really worth it? Should the interface be used for business need instead of only for making code unit testable? Can we have a way to test our code without introducing these tests only interfaces?
That's the reason why injectorpp is here.
Share your thoughts. Happy to discuss
1 post - 1 participant
🏷️ rust_feed