Section IV - module
To create the module, create a new file named hello_world.rs in the /src directory.
To begin, we will follow some basic TDD practices and build our tests first.
NOTE: This is not a TDD workshop, so we will ignore the complete practices and simply illustrate how it would be done.
At the bottom of the file, create an empty nested testing module. This will be where we write our unit test for the hello_world module. The use super::*; line imports all the functionality and variables from the parent hello_world module.
#[cfg(test)]
mod tests {
use super::*;
}Our first test will be to return the service root. Add the following test in tests module below the use super::; line so it looks like the following.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_service_root() {
assert_eq!(get_service_root(), format!("/hello/{}", VER));
}
}Following TDD practices, we now run our test and confirm that it will fail.
To make the test pass, we will add the get_service_root() function to the module.
If we rerun our test, it will now pass.
We will do the same for the get_service_path() function.
Now that we have an understanding of how to write our tests, and then add the functionality to make them pass, we will move on to provide our service call.
Our test will be the following.
In order to make it pass, we will need to import the web service modules, and provide a index() function.
The final file should look like the following.
Rerun the tests to make sure it all passes.
Last updated
Was this helpful?