public async Task Add_Calls_SaveAsync() { // arrange var countryService = new Mock <ICountryService>(MockBehavior.Strict); var controller = new CountryController(countryService.Object) { TempData = new Mock <ITempDataDictionary>().Object }; var countryName = "CountryName"; var model = new AddCountryViewModel { Name = countryName }; countryService.Setup(s => s.ExistsAsync(countryName)).ReturnsAsync(false); countryService.Setup(s => s.AddAsync(countryName)).ReturnsAsync(2); // act var result = await controller.Add(model); // assert Assert.IsAssignableFrom <RedirectToActionResult>(result); var actionName = (result as RedirectToActionResult).ActionName; Assert.Equal(nameof(CountryController.Add), actionName); countryService.VerifyAll(); }
public async Task Add_When_ModelState_Invalid() { // arrange var countryService = new Mock <ICountryService>(MockBehavior.Strict); var controller = new CountryController(countryService.Object) { TempData = new Mock <ITempDataDictionary>().Object }; var countryName = "CountryName"; var model = new AddCountryViewModel { Name = countryName }; controller.ModelState.AddModelError(string.Empty, "Some Extreme Error"); countryService.Setup(s => s.ExistsAsync(countryName)).ReturnsAsync(false); // act var result = await controller.Add(model); // assert Assert.IsAssignableFrom <ViewResult>(result); var resultModel = (result as ViewResult).Model; Assert.NotNull(resultModel); Assert.IsAssignableFrom <AddCountryViewModel>(resultModel); Assert.Equal(model, resultModel); countryService.VerifyAll(); }
public async Task Add_Returns_ViewResult() { // arrange var countryService = new Mock <ICountryService>(MockBehavior.Strict); var controller = new CountryController(countryService.Object) { TempData = new Mock <ITempDataDictionary>().Object }; // act var result = controller.Add(); // assert Assert.IsAssignableFrom <ViewResult>(result); countryService.VerifyAll(); }
public void AddCountryTest() { //Arrange var mockrep = new Mock <ICountry>(); mockrep.Setup(c => c.Add(It.IsAny <Country>())).Returns(true); CountryController countryCntlrobject = new CountryController(mockrep.Object); Country cntobj = new Country() { Id = "USA", Description = "United States Of America" }; //Act var resultActual = countryCntlrobject.Add(cntobj); //Assert var expected = true; Assert.AreEqual(resultActual, expected); }
public async Task Add_When_Name_Exists() { // arrange var countryService = new Mock <ICountryService>(MockBehavior.Strict); var controller = new CountryController(countryService.Object) { TempData = new Mock <ITempDataDictionary>().Object }; var existingName = "CountryName"; var model = new AddCountryViewModel { Name = existingName }; countryService.Setup(s => s.ExistsAsync(existingName)).ReturnsAsync(true); // act var result = await controller.Add(model); // assert Assert.IsAssignableFrom <ViewResult>(result); var resultModel = (result as ViewResult).Model; Assert.NotNull(resultModel); Assert.IsAssignableFrom <AddCountryViewModel>(resultModel); Assert.Equal(model, resultModel); Assert.False(controller.ModelState.IsValid); Assert.True(controller.ModelState.ContainsKey(nameof(AddCountryViewModel.Name))); var stateValues = controller.ModelState.Values.ToList(); Assert.True(stateValues.Count == 1); var valueMessages = stateValues[0].Errors.ToList(); Assert.True(valueMessages.Count == 1); Assert.True(valueMessages[0].ErrorMessage == CountryWithNameExistsMessage); countryService.VerifyAll(); }
public void CountryController_Add_ReturnsAPartialViewResult() { var add = _countryController.Add(); add.Should().BeOfType <PartialViewResult>(); }