public void TestCreateG() { var controller = new ProductManagerController(); var result = controller.Create() as ViewResult; Assert.IsNotNull(result); }
public void ReturnCreateWithProductManagerViewModel() { _mockProductCategoryRepository.Setup(productCategories => productCategories.Collection()) .Returns(GetProductCategories()); _sut = new ProductManagerController(_mockProductRepository.Object, _mockProductCategoryRepository.Object, _mockEnvironment.Object); IActionResult result = _sut.Create(); var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsType <ProductManagerViewModel>(viewResult.Model); Assert.Equal(2, model.ProductCategories.Count()); Assert.Equal("Sneakers", model.ProductCategories.First().Category); }
public void NotSaveProductWhenModelError() { _sut = new ProductManagerController(_mockProductRepository.Object, _mockProductCategoryRepository.Object, _mockEnvironment.Object); _sut.ModelState.AddModelError("x", "Test Error"); var product = new Product { Category = "Sneakers" }; IFormFile file = null; _sut.Create(product, file); _mockProductRepository.Verify( x => x.Add(It.IsAny <Product>()), Times.Never); }
public void ReturnCreateWithProductManagerViewModelWhenInvalidModelState() { _sut = new ProductManagerController(_mockProductRepository.Object, _mockProductCategoryRepository.Object, _mockEnvironment.Object); _sut.ModelState.AddModelError("x", "Test Error"); var product = new Product { Category = "Sneakers" }; IFormFile file = null; IActionResult result = _sut.Create(product, file); var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsType <ProductManagerViewModel>(viewResult.Model); Assert.Equal(product.Category, model.Product.Category); }
public void RedirectToActionIndexWhenValidModelForCreate() { _sut = new ProductManagerController(_mockProductRepository.Object, _mockProductCategoryRepository.Object, _mockEnvironment.Object); var product = new Product { Category = "Sneakers", CreatedAt = DateTimeOffset.Now, Description = "White sneakers", Id = "abc123", Image = "/path/image.jpg", Name = "Nike Air Force One", Price = 10 }; IFormFile file = null; var result = _sut.Create(product, file); var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("Index", redirectToActionResult.ActionName); }
public void SaveProductWhenValidModel() { _sut = new ProductManagerController(_mockProductRepository.Object, _mockProductCategoryRepository.Object, _mockEnvironment.Object); Product savedProduct = null; _mockProductRepository.Setup(x => x.Add(It.IsAny <Product>())) .Callback <Product>(x => savedProduct = x); var product = new Product { Category = "Sneakers", CreatedAt = DateTimeOffset.Now, Description = "White sneakers", Id = "abc123", Image = "/path/image.jpg", Name = "Nike Air Force One", Price = 10 }; IFormFile file = null; _sut.Create(product, file); _mockProductRepository.Verify( x => x.Add(It.IsAny <Product>()), Times.Once); Assert.Equal(product.Category, savedProduct.Category); Assert.Equal(product.CreatedAt, savedProduct.CreatedAt); Assert.Equal(product.Description, savedProduct.Description); Assert.Equal(product.Id, savedProduct.Id); Assert.Equal(product.Image, savedProduct.Image); Assert.Equal(product.Name, savedProduct.Name); Assert.Equal(product.Price, savedProduct.Price); }