Help with the Chapter 12 project in the book

I am a beginner and I'm currently in Chapter 12.4. There is this suggestion there:

Feel free to write some tests for the functionality in the Config::new and run functions on your own.

I am wondering what tests one can write for the run function in the project? The run function doesn't return anything except an error if something goes wrong. Can someone suggest what sort of test I could write here? For reference here is what the run function looks like in my (same as in the book):

  fn run(config: Config) -> Result<(), Box<dyn Error>> {
     let contents = fs::read_to_string(config.filename)?;

    println!("With text:\n{}", contents);


Quite obvious unit test case would be to test whether the function returns an error when it is supposed to. Provide Config instance with non-existent filename and check whether proper error is returned, for example using assert! and matches! macro.

1 Like


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.