Created
February 2, 2012 08:44
-
-
Save alexbeletsky/1722396 to your computer and use it in GitHub Desktop.
Moq and NSubstitute tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using NUnit.Framework; | |
using NSubstitute; | |
using Moq; | |
namespace NewTools | |
{ | |
public class EmailMessage | |
{ | |
public string To { get; set; } | |
public string Bcc { get; set; } | |
public string From { get; set; } | |
public string Subject { get; set; } | |
public string Message { get; set; } | |
public bool IsHtml { get; set; } | |
} | |
public interface IEMailService | |
{ | |
void SendEmail(EmailMessage message, string account); | |
int EmailsInQueue { get; } | |
} | |
public class ActionResult | |
{ | |
} | |
public class SignUpModel | |
{ | |
public string Password { get; set; } | |
public string Email { get; set; } | |
} | |
public class LoginController | |
{ | |
readonly IEMailService _emailService; | |
public LoginController(IEMailService emailService) | |
{ | |
_emailService = emailService; | |
} | |
public ActionResult Login() | |
{ | |
return View(); | |
} | |
public ActionResult SignUp(SignUpModel model) | |
{ | |
if (_emailService.EmailsInQueue < 5) | |
{ | |
_emailService.SendEmail(new EmailMessage(), "current"); | |
} | |
return View(); | |
} | |
private ActionResult View() | |
{ | |
return null; | |
} | |
} | |
public class NewToolsTests | |
{ | |
[Test] | |
public void should_send_an_email_if_users_signs_up_nsub() | |
{ | |
// arrange | |
var model = new SignUpModel { Email = "a@a.com", Password = "xxx" }; | |
var emailService = Substitute.For<IEMailService>(); | |
var controller = new LoginController(emailService); | |
// act | |
controller.SignUp(model); | |
// assert | |
emailService.Received().SendEmail(Arg.Any<EmailMessage>(), "current"); | |
} | |
[Test] | |
public void should_send_an_email_if_user_signs_up_moq() | |
{ | |
// arrange | |
var model = new SignUpModel { Email = "a@a.com", Password = "xxx" }; | |
var emailService = new Mock<IEMailService>(); | |
var controller = new LoginController(emailService.Object); | |
// act | |
controller.SignUp(model); | |
// assert | |
emailService.Verify(x => x.SendEmail(It.IsAny<EmailMessage>(), "current")); | |
} | |
[Test] | |
public void should_not_send_email_if_number_of_email_in_queque_exceed_five_nsub() | |
{ | |
// arrange | |
var model = new SignUpModel { Email = "a@a.com", Password = "xxx" }; | |
var emailService = Substitute.For<IEMailService>(); | |
emailService.EmailsInQueue.Returns(5); | |
var controller = new LoginController(emailService); | |
// act | |
controller.SignUp(model); | |
// assert | |
emailService.DidNotReceiveWithAnyArgs().SendEmail(new EmailMessage(), string.Empty); | |
} | |
[Test] | |
public void should_not_send_email_if_number_of_email_in_queque_exceed_five_moq() | |
{ | |
// arrange | |
var model = new SignUpModel { Email = "a@a.com", Password = "xxx" }; | |
var emailService = new Mock<IEMailService>(); | |
emailService.Setup(x => x.EmailsInQueue).Returns(5); | |
var controller = new LoginController(emailService.Object); | |
// act | |
controller.SignUp(model); | |
// assert | |
emailService.Verify(x => x.SendEmail(It.IsAny<EmailMessage>(), It.IsAny<string>()), Times.Never()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment