public void Send(Email email) { if (email == null) { throw new ArgumentNullException("email"); } if (!_validatorRunner.IsValid(email)) { throw new ValidationException("Email is invalid to send", _validatorRunner.GetErrorSummary(email).ErrorMessages); } var smtpClient = new SmtpClient(); using (var message = new MailMessage()) { message.From = new MailAddress(email.FromEmailAddress); message.To.Add(email.ToEmailAddress); message.Subject = email.Subject; message.Body = email.Message; message.IsBodyHtml = false; smtpClient.Send(message); } }
public void Should_Fail_To_Send_Email_Because_Email_Is_Invalid() { // arrange const string errorMessage = "message"; var email = new Email(); var validatorRunner = new Mock<IValidatorRunner>(); var errorSummary = new ErrorSummary(); validatorRunner.Setup(v => v.IsValid(It.IsAny<Email>())).Returns(false); validatorRunner.Setup(v => v.GetErrorSummary(It.IsAny<Email>())).Returns(errorSummary); errorSummary.RegisterErrorMessage(errorMessage, errorMessage); // act var service = new EmailSenderService(validatorRunner.Object); TestDelegate act = () => service.Send(email); // assert Assert.That(act, Throws.InstanceOf<ValidationException>().With.Property("ValidationErrorMessages").EqualTo(new[] { errorMessage })); }