public void Buy_WhenModelMinPurchaseAmountIsGreaterThanMaxPurchaseAmount_ReturnViewResultBuyPracticePage() { var emailSender = new Mock <ISendEmail>().Object; var tempData = new Mock <ITempDataDictionary>().Object; var reCaptcha = MockReCaptchaService.Create(true); var sut = new PracticeController(Enumerable.Empty <IGenerateEmail>(), emailSender, reCaptcha); sut.TempData = tempData; var fixture = new Fixture(); var model = fixture .Build <BuyPracticeModel>() .Without(m => m.ContactPhoneNumber) .Create(); model.MinPurchaseAmount = 100; model.MaxPurchaseAmount = 99; var result = sut.Buy(model); Assert.IsType <Task <IActionResult> >(result); var viewResult = result.Result as ViewResult; Assert.Equal("Buy", viewResult?.ViewName); }
public void ContactUs_WhenThereAreMultipleEmailGenerators_OnlyGeneratesContactUsEmail() { var emailSender = new Mock <ISendEmail>().Object; var mockContactUsGenerator = new Mock <IGenerateEmail>(); mockContactUsGenerator.Setup(m => m.CanGenerateEmailFromModel(It.IsAny <object>())) .Returns <object>(o => o is ContactUsModel); mockContactUsGenerator.Setup(m => m.GenerateEmail(It.IsAny <object>())) .Verifiable("Email was not generated."); var mockSellNotificationGenerator = new Mock <IGenerateEmail>(); mockSellNotificationGenerator.Setup(m => m.CanGenerateEmailFromModel(It.IsAny <object>())) .Returns <object>(o => o is SellPracticeModel); mockSellNotificationGenerator.Setup(m => m.GenerateEmail(It.IsAny <object>())) .Verifiable("Incorrect email was generated."); var mockBuyNotificationGenerator = new Mock <IGenerateEmail>(); mockBuyNotificationGenerator.Setup(m => m.CanGenerateEmailFromModel(It.IsAny <object>())) .Returns <object>(o => o is BuyPracticeModel); mockBuyNotificationGenerator.Setup(m => m.GenerateEmail(It.IsAny <object>())) .Verifiable("Incorrect email was generated."); var emailGenerators = new[] { mockContactUsGenerator.Object, mockBuyNotificationGenerator.Object, mockSellNotificationGenerator.Object }; var sut = new HomeController(emailGenerators, emailSender, MockReCaptchaService.Create(true)); Assert.True(sut.ModelState.IsValid); var fixture = new Fixture(); var model = fixture.Build <ContactUsModel>() .With(m => m.PhoneNumber, RandomDataGenerator.RandomPhoneNumber()) .Create(); //Act // ReSharper disable once UnusedVariable var result = sut.ContactUs(model); // Assert -- Generally we don't want to test implementations but rather test results. // The result we are testing here is that the controller calls the correct // generator and is not calling all the generators mockContactUsGenerator.Verify(m => m.GenerateEmail(It.IsAny <object>()), Times.Exactly(1)); mockBuyNotificationGenerator.Verify(m => m.GenerateEmail(It.IsAny <object>()), Times.Never); mockSellNotificationGenerator.Verify(m => m.GenerateEmail(It.IsAny <object>()), Times.Never); }
public void Buy_WhenModelIsValid_ReturnsRedirectToActionResultThankyouPage() { var emailSender = new Mock <ISendEmail>().Object; var reCaptcha = MockReCaptchaService.Create(true); var sut = new PracticeController(Enumerable.Empty <IGenerateEmail>(), emailSender, reCaptcha); var result = sut.Buy(new BuyPracticeModel()); Assert.IsType <Task <IActionResult> >(result); var redirectToActionResult = result.Result as RedirectToActionResult; Assert.Equal("ThankYou", redirectToActionResult?.ControllerName); Assert.Equal("Index", redirectToActionResult?.ActionName); }
public void Buy_WhenReCaptchaFails_ReturnViewResultBuyPracticePage() { var emailSender = new Mock <ISendEmail>().Object; var tempData = new Mock <ITempDataDictionary>().Object; var reCaptcha = MockReCaptchaService.Create(false); var sut = new PracticeController(Enumerable.Empty <IGenerateEmail>(), emailSender, reCaptcha); sut.TempData = tempData; var result = sut.Buy(new BuyPracticeModel()); Assert.IsType <Task <IActionResult> >(result); var viewResult = result.Result as ViewResult; Assert.Equal("Buy", viewResult?.ViewName); }
public void Sell_WhenModelIsInvalid_ReturnViewResultSellPracticePage() { var emailSender = new Mock <ISendEmail>().Object; var tempData = new Mock <ITempDataDictionary>().Object; var reCaptcha = MockReCaptchaService.Create(true); var sut = new PracticeController(Enumerable.Empty <IGenerateEmail>(), emailSender, reCaptcha); sut.ModelState.AddModelError("ModelInvalid", "Test Invalid Model Error"); sut.TempData = tempData; var result = sut.Sell(new SellPracticeModel()); Assert.IsType <Task <IActionResult> >(result); var viewResult = result.Result as ViewResult; Assert.Equal("Sell", viewResult?.ViewName); }
public void Buy_WhenModelMinPurchaseAmountIsLessThanMaxPurchaseAmount_ModelStateIsValid() { var emailSender = new Mock <ISendEmail>().Object; var reCaptcha = MockReCaptchaService.Create(true); var sut = new PracticeController(Enumerable.Empty <IGenerateEmail>(), emailSender, reCaptcha); var fixture = new Fixture(); var model = fixture .Build <BuyPracticeModel>() .Without(m => m.ContactPhoneNumber) .With(m => m.MinPurchaseAmount, 99) .With(m => m.MaxPurchaseAmount, 100) .Create(); Assert.True(sut.ModelState.IsValid); var unused = sut.Buy(model); Assert.True(sut.ModelState.IsValid); }
public void Buy_WhenModelMinPurchaseAmountIsLessThanMaxPurchaseAmount_ReturnsRedirectToActionResultThankyouPage() { var emailSender = new Mock <ISendEmail>().Object; var reCaptcha = MockReCaptchaService.Create(true); var sut = new PracticeController(Enumerable.Empty <IGenerateEmail>(), emailSender, reCaptcha); var fixture = new Fixture(); var model = fixture .Build <BuyPracticeModel>() .Without(m => m.ContactPhoneNumber) .With(m => m.MinPurchaseAmount, 99) .With(m => m.MaxPurchaseAmount, 100) .Create(); var result = sut.Buy(model); Assert.IsType <Task <IActionResult> >(result); var redirectToActionResult = result.Result as RedirectToActionResult; Assert.Equal("ThankYou", redirectToActionResult?.ControllerName); Assert.Equal("Index", redirectToActionResult?.ActionName); }
public void ContactUs_WhenModelIsValid_GeneratesEmail() { var emailSender = new Mock <ISendEmail>().Object; var mockEmailGenerator = new Mock <IGenerateEmail>(); mockEmailGenerator.Setup(m => m.CanGenerateEmailFromModel(It.IsAny <object>())) .Returns(true); mockEmailGenerator.Setup(m => m.GenerateEmail(It.IsAny <object>())) .Verifiable("Email was not generated."); var sut = new HomeController(new [] { mockEmailGenerator.Object }, emailSender, MockReCaptchaService.Create(true)); Assert.True(sut.ModelState.IsValid); var fixture = new Fixture(); var model = fixture.Build <ContactUsModel>() .With(m => m.PhoneNumber, RandomDataGenerator.RandomPhoneNumber()) .Create(); //Act // ReSharper disable once UnusedVariable var result = sut.ContactUs(model); // Assert -- Generally we don't want to test implementations but rather test results. // The result we are testing for is to verify that the controller is // generating an email not how it is generating the email. mockEmailGenerator.Verify(); }
public void ContactUs_WhenModelIsValid_ReturnsContactUsView() { var emailSender = new Mock <ISendEmail>().Object; var sut = new HomeController(Enumerable.Empty <IGenerateEmail>(), emailSender, MockReCaptchaService.Create(true)); Assert.True(sut.ModelState.IsValid); var fixture = new Fixture(); var model = fixture.Build <ContactUsModel>() .With(m => m.PhoneNumber, RandomDataGenerator.RandomPhoneNumber()) .Create(); //Act // ReSharper disable once UnusedVariable var result = sut.ContactUs(model); // Assert Assert.IsType <Task <IActionResult> >(result); var redirectToActionResult = result.Result as RedirectToActionResult; Assert.Equal(nameof(ThankYouController), redirectToActionResult?.ControllerName + "Controller"); Assert.Equal(nameof(ThankYouController.Index), redirectToActionResult?.ActionName); }
public void ContactUs_WhenModelIsNotValid_ReturnsContactUsView() { var emailSender = new Mock <ISendEmail>().Object; var tempData = new Mock <ITempDataDictionary>().Object; var sut = new HomeController(Enumerable.Empty <IGenerateEmail>(), emailSender, MockReCaptchaService.Create(true)); sut.ModelState.AddModelError("Test", "Test Error"); sut.TempData = tempData; Assert.False(sut.ModelState.IsValid); var fixture = new Fixture(); var model = fixture.Build <ContactUsModel>() .With(m => m.PhoneNumber, RandomDataGenerator.RandomPhoneNumber()) .Create(); //Act // ReSharper disable once UnusedVariable var result = sut.ContactUs(model); // Assert Assert.IsType <Task <IActionResult> >(result); var viewResult = result.Result as ViewResult; Assert.Equal("ContactUs", viewResult?.ViewName); }
public void ContactUs_WhenReCaptchaIsNotValid_AddsErrorToModelState() { var emailSender = new Mock <ISendEmail>().Object; var sut = new HomeController(Enumerable.Empty <IGenerateEmail>(), emailSender, MockReCaptchaService.Create(false)); Assert.True(sut.ModelState.IsValid); var fixture = new Fixture(); var model = fixture.Build <ContactUsModel>() .With(m => m.PhoneNumber, RandomDataGenerator.RandomPhoneNumber()) .Create(); //Act // ReSharper disable once UnusedVariable var result = sut.ContactUs(model); // Assert Assert.False(sut.ModelState.IsValid); Assert.Contains(sut.ModelState.Keys, s => s == "reCaptchaError"); }