From 64cf53f7b40f65c9dcbba6c91a3e591732da6b8a Mon Sep 17 00:00:00 2001 From: Ahmed Kamal Date: Sun, 17 Nov 2024 00:15:17 +0200 Subject: [PATCH] Refactor EmailMessage and tests for simplified initialization Implemented property initializers in EmailSender and EmailRecipient using primary constructors for a more concise and clear initialization. Refactored relevant test methods to use expression-bodied members for consistency and improved readability. --- src/MailFusion/EmailMessage.cs | 16 +++++-------- test/MailFusion.Tests/EmailServiceTests.cs | 26 +++++++++------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/src/MailFusion/EmailMessage.cs b/src/MailFusion/EmailMessage.cs index fe1765a..8d001ea 100644 --- a/src/MailFusion/EmailMessage.cs +++ b/src/MailFusion/EmailMessage.cs @@ -1,3 +1,5 @@ +using System.Diagnostics.CodeAnalysis; + namespace MailFusion; /// @@ -209,12 +211,9 @@ public EmailSender() /// ); /// /// + [SetsRequiredMembers] public EmailSender(string email, string name, string? replyEmail = null) - { - Email = email; - Name = name; - ReplyEmail = replyEmail ?? email; - } + => (Email, Name, ReplyEmail) = (email, name, replyEmail ?? email); } /// @@ -310,9 +309,6 @@ public EmailRecipient() /// ); /// /// - public EmailRecipient(string email, string? name = null) - { - Email = email; - Name = name; - } + [SetsRequiredMembers] + public EmailRecipient(string email, string? name = null) => (Email, Name) = (email, name); } \ No newline at end of file diff --git a/test/MailFusion.Tests/EmailServiceTests.cs b/test/MailFusion.Tests/EmailServiceTests.cs index c224c83..f901dbd 100644 --- a/test/MailFusion.Tests/EmailServiceTests.cs +++ b/test/MailFusion.Tests/EmailServiceTests.cs @@ -98,9 +98,8 @@ public async Task SendFromTemplateAsync_WithEmptyRecipients_ShouldReturnFailure( result.Error!.Code.Should().Be(EmailServiceErrors.Codes.InvalidInput); } - private static EmailMessage CreateValidEmailMessage() - { - return new EmailMessage + private static EmailMessage CreateValidEmailMessage() => + new() { Subject = "Test Subject", HtmlBody = "

Test Body

", @@ -108,32 +107,27 @@ private static EmailMessage CreateValidEmailMessage() Sender = CreateValidSender(), Recipients = CreateValidRecipients() }; - } - private static EmailSender CreateValidSender() - { - return new EmailSender + private static EmailSender CreateValidSender() => + new() { Name = "Test Sender", Email = "sender@test.com", ReplyEmail = "reply@test.com" }; - } - private static List CreateValidRecipients() - { - return [new EmailRecipient { Email = "recipient@test.com", Name = "Test Recipient" }]; - } + private static List CreateValidRecipients() => + [ + new("recipient@test.com", "Test Recipient") + ]; - private static EmailTemplate CreateValidTemplate() - { - return new EmailTemplate + private static EmailTemplate CreateValidTemplate() => + new() { Subject = "Test Subject", HtmlBody = "

Test Body

", PlainTextBody = "Test Body" }; - } private class TestEmailModel : IEmailTemplateModel {