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

@ -3,8 +3,7 @@ use crate::helpers::{spawn_app, TestApp};
#[tokio::test]
async fn subscribe_returns_a_422_when_data_is_missing() {
// Arrange
let TestApp { address, .. } = spawn_app().await;
let client = reqwest::Client::new();
let app = spawn_app().await;
let test_cases = vec![
("name=le%20guin", "missing the email"),
("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 {
// Act
let response = client
.post(&format!("{address}/subscriptions"))
.header("Content-Type", "application/x-www-form-urlencoded")
.body(invalid_body)
.send()
.await
.expect("Failed to execute request.");
let response = app.post_subscriptions(invalid_body.into()).await;
// Assert
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() {
// Arrange
let app = spawn_app().await;
let client = reqwest::Client::new();
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
// Act
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
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.");
let response = app.post_subscriptions(body.into()).await;
// Assert
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() {
// Arrange
let app = spawn_app().await;
let client = reqwest::Client::new();
let test_cases = vec![
("name=&email=ursula_le_guin%40gmail.com", "empty name"),
("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 {
// Act
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.");
let response = app.post_subscriptions(body.into()).await;
// Assert
assert_eq!(