I'm afraid this is a very newbie question, but I've been fighting the compiler for too long, so I've come here to ask for help :-). I've simplified my scenario to capture only what I'm stuck with.
Say I have a struct:
pub struct Worker{
id: u32,
filename: String,
}
This struct has a method to handle writes to the file system (along with its constructor):
impl Worker {
pub fn new(
id: u32,
filename: String,
) -> Worker {
Worker {
id: id,
filename: filename,
}
}
pub fn read_file(&self) -> io::Result<String> {
Ok(fs::read_to_string(&mut Path::new(&self.filename))?)
}
pub fn do_task(&self) -> Result<(), MyError> {
// -- sniip
let mut new_file = File::create(self.filename).map_err(MyError::Io)?;
// -- sniip
}
Say I wanna test do_task()
for the case where File::create
fails, and I wanna check that the error I got was indeed MyError::Io
.
The way I've been trying to do it was to pass a Write
trait to my struct (or something like that) so I don't call File::create
directly and instead use whatever was passed to Worker::new(...)
. During my tests I'd pass a mocked version that could trigger the FS error. But I've tried many, many ways of doing that but I can't seem to make it work, all I want is something like passing mut writer: impl std::io::Write
to Worker::new()
. How would you folks approach this?
Thank you so much in advance!