tests: remove hidden couplings
This commit is contained in:
parent
ebd7755731
commit
fe5a596265
3 changed files with 27 additions and 35 deletions
|
|
@ -1,17 +1,12 @@
|
|||
use crate::helpers::{spawn_app, TestApp};
|
||||
use crate::helpers::spawn_app;
|
||||
|
||||
#[tokio::test]
|
||||
async fn health_check_works() {
|
||||
// Arrange
|
||||
let TestApp { address, .. } = spawn_app().await;
|
||||
let app = spawn_app().await;
|
||||
|
||||
// Act
|
||||
let client = reqwest::Client::new();
|
||||
let response = client
|
||||
.get(format!("{address}/health_check"))
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to execute request.");
|
||||
let response = app.get_health_check().await;
|
||||
|
||||
// Assert
|
||||
assert!(response.status().is_success());
|
||||
|
|
|
|||
|
|
@ -27,6 +27,25 @@ pub struct TestApp {
|
|||
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 {
|
||||
// The first time `initialize` is invoked the code in `TRACING` is executed.
|
||||
// All other invocations will instead skip execution.
|
||||
|
|
@ -60,7 +79,6 @@ pub async fn spawn_app() -> TestApp {
|
|||
|
||||
TestApp {
|
||||
address,
|
||||
// address: format!("http://localhost:{}", application_port),
|
||||
// port: application_port,
|
||||
db_pool: connection_pool,
|
||||
// email_server,
|
||||
|
|
|
|||
|
|
@ -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!(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue