Refactor tests.

This commit is contained in:
LukeMathWalker 2021-01-17 00:43:04 +00:00
parent 74b7b1c536
commit b40748ef7b

View file

@ -86,16 +86,31 @@ mod tests {
}
}
/// Generate a random email subject
fn subject() -> String {
Sentence(1..2).fake()
}
/// Generate a random email content
fn content() -> String {
Paragraph(1..10).fake()
}
/// Generate a random subscriber email
fn email() -> SubscriberEmail {
SubscriberEmail::parse(SafeEmail().fake()).unwrap()
}
/// Get a test instance of `EmailClient`.
fn email_client(base_url: String) -> EmailClient {
let email_client = EmailClient::new(base_url, email(), Faker.fake());
}
#[tokio::test]
async fn send_email_sends_the_expected_request() {
// Arrange
let mock_server = MockServer::start().await;
let sender = SubscriberEmail::parse(SafeEmail().fake()).unwrap();
let email_client = EmailClient::new(mock_server.uri(), sender, Faker.fake());
let subscriber_email = SubscriberEmail::parse(SafeEmail().fake()).unwrap();
let subject: String = Sentence(1..2).fake();
let content: String = Paragraph(1..10).fake();
let email_client = email_client(mock_server.uri());
Mock::given(header_exists("X-Postmark-Server-Token"))
.and(header("Content-Type", "application/json"))
@ -109,7 +124,7 @@ mod tests {
// Act
let _ = email_client
.send_email(subscriber_email, &subject, &content, &content)
.send_email(subscriber_email(), &subject(), &content(), &content())
.await;
// Assert
@ -119,12 +134,7 @@ mod tests {
async fn send_email_succeeds_if_the_server_returns_200() {
// Arrange
let mock_server = MockServer::start().await;
let sender = SubscriberEmail::parse(SafeEmail().fake()).unwrap();
let email_client = EmailClient::new(mock_server.uri(), sender, Faker.fake());
let subscriber_email = SubscriberEmail::parse(SafeEmail().fake()).unwrap();
let subject: String = Sentence(1..2).fake();
let content: String = Paragraph(1..10).fake();
let email_client = email_client(mock_server.uri());
Mock::given(any())
.respond_with(ResponseTemplate::new(200))
@ -134,7 +144,7 @@ mod tests {
// Act
let outcome = email_client
.send_email(subscriber_email, &subject, &content, &content)
.send_email(subscriber_email(), &subject(), &content(), &content())
.await;
// Assert
@ -145,12 +155,7 @@ mod tests {
async fn send_email_fails_if_the_server_returns_500() {
// Arrange
let mock_server = MockServer::start().await;
let sender = SubscriberEmail::parse(SafeEmail().fake()).unwrap();
let email_client = EmailClient::new(mock_server.uri(), sender, Faker.fake());
let subscriber_email = SubscriberEmail::parse(SafeEmail().fake()).unwrap();
let subject: String = Sentence(1..2).fake();
let content: String = Paragraph(1..10).fake();
let email_client = email_client(mock_server.uri());
Mock::given(any())
// Not a 200 anymore!
@ -161,7 +166,7 @@ mod tests {
// Act
let outcome = email_client
.send_email(subscriber_email, &subject, &content, &content)
.send_email(subscriber_email(), &subject(), &content(), &content())
.await;
// Assert
@ -172,12 +177,7 @@ mod tests {
async fn send_email_times_out_if_the_server_takes_too_long() {
// Arrange
let mock_server = MockServer::start().await;
let sender = SubscriberEmail::parse(SafeEmail().fake()).unwrap();
let email_client = EmailClient::new(mock_server.uri(), sender, Faker.fake());
let subscriber_email = SubscriberEmail::parse(SafeEmail().fake()).unwrap();
let subject: String = Sentence(1..2).fake();
let content: String = Paragraph(1..10).fake();
let email_client = email_client(mock_server.uri());
let response = ResponseTemplate::new(200).set_delay(std::time::Duration::from_secs(180));
Mock::given(any())
@ -188,7 +188,7 @@ mod tests {
// Act
let outcome = email_client
.send_email(subscriber_email, &subject, &content, &content)
.send_email(subscriber_email(), &subject(), &content(), &content())
.await;
// Assert