tests: remove hidden couplings

This commit is contained in:
Sandro Eiler 2024-03-04 11:48:47 +01:00
parent ebd7755731
commit fe5a596265
3 changed files with 27 additions and 35 deletions

View file

@ -1,17 +1,12 @@
use crate::helpers::{spawn_app, TestApp}; use crate::helpers::spawn_app;
#[tokio::test] #[tokio::test]
async fn health_check_works() { async fn health_check_works() {
// Arrange // Arrange
let TestApp { address, .. } = spawn_app().await; let app = spawn_app().await;
// Act // Act
let client = reqwest::Client::new(); let response = app.get_health_check().await;
let response = client
.get(format!("{address}/health_check"))
.send()
.await
.expect("Failed to execute request.");
// Assert // Assert
assert!(response.status().is_success()); assert!(response.status().is_success());

View file

@ -27,6 +27,25 @@ pub struct TestApp {
pub db_pool: PgPool, pub db_pool: PgPool,
} }
impl TestApp {
pub async fn get_health_check(&self) -> reqwest::Response {
reqwest::Client::new()
.get(&format!("{}/health_check", &self.address))
.send()
.await
.expect("Failed to execute request.")
}
pub async fn post_subscriptions(&self, body: String) -> reqwest::Response {
reqwest::Client::new()
.post(&format!("{}/subscriptions", &self.address))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(body)
.send()
.await
.expect("Failed to execute request.")
}
}
pub async fn spawn_app() -> TestApp { pub async fn spawn_app() -> TestApp {
// The first time `initialize` is invoked the code in `TRACING` is executed. // The first time `initialize` is invoked the code in `TRACING` is executed.
// All other invocations will instead skip execution. // All other invocations will instead skip execution.
@ -60,7 +79,6 @@ pub async fn spawn_app() -> TestApp {
TestApp { TestApp {
address, address,
// address: format!("http://localhost:{}", application_port),
// port: application_port, // port: application_port,
db_pool: connection_pool, db_pool: connection_pool,
// email_server, // email_server,

View file

@ -3,8 +3,7 @@ use crate::helpers::{spawn_app, TestApp};
#[tokio::test] #[tokio::test]
async fn subscribe_returns_a_422_when_data_is_missing() { async fn subscribe_returns_a_422_when_data_is_missing() {
// Arrange // Arrange
let TestApp { address, .. } = spawn_app().await; let app = spawn_app().await;
let client = reqwest::Client::new();
let test_cases = vec![ let test_cases = vec![
("name=le%20guin", "missing the email"), ("name=le%20guin", "missing the email"),
("email=ursula_le_guin%40gmail.com", "missing the name"), ("email=ursula_le_guin%40gmail.com", "missing the name"),
@ -13,13 +12,7 @@ async fn subscribe_returns_a_422_when_data_is_missing() {
for (invalid_body, error_message) in test_cases { for (invalid_body, error_message) in test_cases {
// Act // Act
let response = client let response = app.post_subscriptions(invalid_body.into()).await;
.post(&format!("{address}/subscriptions"))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(invalid_body)
.send()
.await
.expect("Failed to execute request.");
// Assert // Assert
assert_eq!( assert_eq!(
@ -35,17 +28,10 @@ async fn subscribe_returns_a_422_when_data_is_missing() {
async fn subscribe_returns_a_200_for_valid_form_data() { async fn subscribe_returns_a_200_for_valid_form_data() {
// Arrange // Arrange
let app = spawn_app().await; let app = spawn_app().await;
let client = reqwest::Client::new(); let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
// Act // Act
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com"; let response = app.post_subscriptions(body.into()).await;
let response = client
.post(&format!("{}/subscriptions", &app.address))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(body)
.send()
.await
.expect("Failed to execute request.");
// Assert // Assert
assert_eq!(200, response.status().as_u16()); assert_eq!(200, response.status().as_u16());
@ -63,7 +49,6 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() { async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() {
// Arrange // Arrange
let app = spawn_app().await; let app = spawn_app().await;
let client = reqwest::Client::new();
let test_cases = vec![ let test_cases = vec![
("name=&email=ursula_le_guin%40gmail.com", "empty name"), ("name=&email=ursula_le_guin%40gmail.com", "empty name"),
("name=Ursula&email=", "empty email"), ("name=Ursula&email=", "empty email"),
@ -72,13 +57,7 @@ async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() {
for (body, description) in test_cases { for (body, description) in test_cases {
// Act // Act
let response = client let response = app.post_subscriptions(body.into()).await;
.post(&format!("{}/subscriptions", &app.address))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(body)
.send()
.await
.expect("Failed to execute request.");
// Assert // Assert
assert_eq!( assert_eq!(