private ProductsController GetController() { var testProduct = DomainTestUtil.GetTestProducts(1)[0]; testProduct.Category = DomainTestUtil.GetTestCategories(1)[0]; var dataManagerMock = new Mock <IDataManager>(); dataManagerMock.Setup(x => x.ProductRepository.GetAllEntities()) .Returns(DomainTestUtil.GetTestProducts()); dataManagerMock.Setup(x => x.ProductRepository.GetEntity(It.IsAny <Guid>())) .Returns <Guid>(a => testProduct); dataManagerMock.Setup(x => x.ProductRepository.SaveEntity(It.IsAny <Product>())) .Returns <Product>(a => Guid.NewGuid()); dataManagerMock.Setup(x => x.CategoryRepository.GetAllEntities()) .Returns(DomainTestUtil.GetTestCategories()); dataManagerMock.Setup(x => x.CategoryRepository.GetEntity(It.IsAny <Guid>())) .Returns <Guid>(a => DomainTestUtil.GetTestCategories(1)[0]); var imageServiceMock = new Mock <IImageService>(); var environmentMock = new Mock <IWebHostEnvironment>(); return(new ProductsController(dataManagerMock.Object, imageServiceMock.Object, environmentMock.Object) { ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object }); }
private CategoryController GetController(bool returnsNullCategory = false) { var dataManagerMock = new Mock <IDataManager>(); dataManagerMock.Setup(x => x.CategoryRepository.GetEntity(It.IsAny <Guid>())) .Returns <Guid>(a => returnsNullCategory ? null : DomainTestUtil.GetTestCategories(1)[0]); return(new CategoryController(dataManagerMock.Object)); }
private CategoriesController GetController() { var dataManagerMock = new Mock <IDataManager>(); dataManagerMock.Setup(x => x.CategoryRepository.GetAllEntities()) .Returns(DomainTestUtil.GetTestCategories()); dataManagerMock.Setup(x => x.CategoryRepository.GetEntity(It.IsAny <Guid>())) .Returns <Guid>(a => DomainTestUtil.GetTestCategories(1)[0]); dataManagerMock.Setup(x => x.CategoryRepository.SaveEntity(It.IsAny <Category>())) .Returns <Category>(a => Guid.NewGuid()); return(new CategoriesController(dataManagerMock.Object) { ObjectValidator = ControllerTestUtil.GetObjectModelValidatorMock().Object }); }
public void IndexGetTest() { var dataManagerMock = new Mock <IDataManager>(); dataManagerMock.Setup(x => x.ProductRepository.GetAllEntities()) .Returns(DomainTestUtil.GetTestProducts()); dataManagerMock.Setup(x => x.CategoryRepository.GetAllEntities()) .Returns(DomainTestUtil.GetTestCategories()); var controller = new SearchController(dataManagerMock.Object); var result = controller.Index("query", "filter"); var viewResult = Assert.IsType <ViewResult>(result); Assert.Equal("~/Views/ListView.cshtml", viewResult.ViewName); var model = Assert.IsType <ListViewModel>(viewResult.Model); Assert.True(model.Products.Count < 9); }
public void IndexTest() { var mock = new Mock <IDataManager>(); var categories = DomainTestUtil.GetTestCategories(); mock.Setup(x => x.CategoryRepository.GetAllEntities()) .Returns(categories); var homeController = new HomeController(mock.Object); var result = homeController.Index(); var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsType <HomeViewModel>(viewResult.Model); foreach (var item in model.AllCategories) { Assert.True(item.BestRatedProducts.Count <= 5); } }