Skip to content

Instantly share code, notes, and snippets.

@ThiagoBarradas
Last active March 6, 2023 17:50
Show Gist options
  • Save ThiagoBarradas/74e019c51e85a01cc92112d4299e3715 to your computer and use it in GitHub Desktop.
Save ThiagoBarradas/74e019c51e85a01cc92112d4299e3715 to your computer and use it in GitHub Desktop.
xUnit Theory MemberData Sample
// models for example
public class SendEmailModel
{
public string Content { get; set; }
public string Email { get; set; }
}
public class SendEmailResult
{
public bool Result { get; set; }
}
// test parameters
public static IEnumerable<object[]> SendEmailParameters()
{
// parameters to test send email success
yield return new object[]
{
new SendEmailModel
{
Content = "hello world",
Email = "user@provider.com"
},
new SendEmailResult
{
Result = true
}
};
// parameters to test send email failed
yield return new object[]
{
new SendEmailModel
{
Content = "hello world",
Email = "wrong-email"
},
new SendEmailResult
{
Result = false
}
};
}
// tests
[Theory]
[MemberData(nameof(SendEmailParameters))]
public static void SendEmail_Should_Validate_Email(SendEmailModel model, SendEmailResult expected)
{
// arrange
var emailSender = new EmailSender();
// act
var emailSended = emailSender.SendEmail(model);
// assert
Assert.Equals(expected.Result, emailSended.Result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment