With the utilities we've created so far in this chapter, the unit tests look pretty compact. To test the handler of the /signup path that expects UserForm, we will add a test_signup_with_client function with the #[test] attribute:
#[test]
fn test_signup_with_client() {
let user = UserForm {
email: "[email protected]".into(),
password: "abc".into(),
};
test_post("/signup", &user);
}
When we run the cargo test command, this function will be called and the test_post call, in turn, will bootstrap a server with a mock server as well.
To test a handler of the /signin path, we will use the following function:
#[test]
fn test_signin_with_client() {
let user = UserForm {
email: "[email protected]".into(),
password: "abc".into(),
};
test_post("/signin", &user);
}
This test uses the same input values with a POST request.
To fetch a list of comments, it's enough to call the test_get function with the /comments path:
#[test]
fn test_list_with_client() {
let _: Vec<Comment> = test_get("/comments");
}
Now, we can start these tests to check the router microservice that forwards requests to a mock server.