feat(test): server setup and teardown for tests

- Application server will now be setup on EVERY test and closed after
that test
This commit is contained in:
2024-03-29 15:36:54 -04:00
parent 8d90f9e8e6
commit cd677039b1
3 changed files with 24 additions and 12 deletions

View File

@@ -1,6 +1,10 @@
// tokio spins up a new async runtime every time
// at the beginning of each test case and shutdown at
// the end of each test case
// the spawn_app() function therefore only survives as long as the runtime
#[tokio::test]
async fn health_check_works(){
spawn_app().await.expect("Failed to spawn our app.");
spawn_app();
let client = reqwest::Client::new();
@@ -10,6 +14,11 @@ async fn health_check_works(){
assert_eq!(Some(0), response.content_length());
}
async fn spawn_app() -> Result<(), std::io::Error> {
email_newsletter_api::run().await
fn spawn_app() {
let server = email_newsletter_api::run().expect("Failed to bind address");
// run() returns an instance of HttpServer that will run forever.
// We don't want this behavior
// Therefore we want to spawn our server, run our test logic
// and then tear down the entire test suite
let _ = tokio::spawn(server);
}