Skip to content

Commit

Permalink
Refactor EmailMessage and tests for simplified initialization
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ahmedkamalio committed Nov 16, 2024
1 parent d8b0ca9 commit 64cf53f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 26 deletions.
16 changes: 6 additions & 10 deletions src/MailFusion/EmailMessage.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Diagnostics.CodeAnalysis;

namespace MailFusion;

/// <summary>
Expand Down Expand Up @@ -209,12 +211,9 @@ public EmailSender()
/// );
/// </code>
/// </example>
[SetsRequiredMembers]
public EmailSender(string email, string name, string? replyEmail = null)
{
Email = email;
Name = name;
ReplyEmail = replyEmail ?? email;
}
=> (Email, Name, ReplyEmail) = (email, name, replyEmail ?? email);
}

/// <summary>
Expand Down Expand Up @@ -310,9 +309,6 @@ public EmailRecipient()
/// );
/// </code>
/// </example>
public EmailRecipient(string email, string? name = null)
{
Email = email;
Name = name;
}
[SetsRequiredMembers]
public EmailRecipient(string email, string? name = null) => (Email, Name) = (email, name);
}
26 changes: 10 additions & 16 deletions test/MailFusion.Tests/EmailServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,42 +98,36 @@ 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 = "<p>Test Body</p>",
PlainTextBody = "Test Body",
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<EmailRecipient> CreateValidRecipients()
{
return [new EmailRecipient { Email = "recipient@test.com", Name = "Test Recipient" }];
}
private static List<EmailRecipient> CreateValidRecipients() =>
[
new("recipient@test.com", "Test Recipient")
];

private static EmailTemplate CreateValidTemplate()
{
return new EmailTemplate
private static EmailTemplate CreateValidTemplate() =>
new()
{
Subject = "Test Subject",
HtmlBody = "<p>Test Body</p>",
PlainTextBody = "Test Body"
};
}

private class TestEmailModel : IEmailTemplateModel
{
Expand Down

0 comments on commit 64cf53f

Please sign in to comment.