У меня есть приложение MVC3, и я получил Register ActionMethod на AccountController. Когда пользователь регистрируется, я хочу, чтобы он отправлял им по электронной почте их данные Я использую Почтовый (впервые) и хочу запустить модульный тест, который проверяет, вызван ли метод Send и что я вызвал шаблон электронной почты Register.
Вот фрагмент кода в моем Регистре ActionMethod, который вызывает шаблон электронной почты:
dynamic email = new Email("Register");
email.To = model.Email;
_emailService.Send(email);
_emailService относится к типу IEmailService. Я использую Moq и знаю, как проверить, был ли вызван метод, но в этом случае я передаю объект Email в Send. Так что я не знаю, создаю ли я Mock или объект Email, используемый на контроллере Register, это то, что я должен тестировать. Я также не уверен в том, как лучше всего проверить «выходные данные» электронного письма или хотя бы убедиться, что шаблон «Register» в папке Views / Emails вызывается. Вот мой тест на данный момент:
[TestMethod]
public void register_post_success_sends_email_with_account_details()
{
// Arrange
// fakeMembers contains a List<Member> repository of 5 fake Members which I
// use to ensure the Register completes and then steps into the code
// that calls the email code authentication code
FakeMembers fakeMembers = new FakeMembers();
_mockMemberRepository.Setup(x => x.GetMembers()).Returns(fakeMembers.Members);
_mockEmailService.Setup(e => e.Send(?? what do I place here??));
// set up of emailService.Send is a void method so do I just not use Returns?
//// Arrange
Member model = new Member()
{
Email = "member6@mydomain.com",
Password = "test123",
ConfirmPassword = "test123"
};
Mock<Email> mockEmail = new Mock<Email>();
// Act
var result = _controller.Register(model);
// Assert
// Do I use a mockEmail.Object here? I want to test the Email called on the
// Register ActionMethod?
// Test Send method was called ONCE
_mockEmailService.Verify(e => e.Send(mockEmail.Object), Times.Once());
//Test either the output of the Email that was sent OR check that it was the
//Register email template that was called
}
Надеюсь, я был достаточно лаконичен, и кто-то может помочь