public async Task AvailableProductsShouldReturnView() { // arrange var mockRepo = new Mock <IUnitOfWork>(); mockRepo.Setup(x => x.StoreRepository.All()) .ReturnsAsync(new List <Store> { new Store { StoreId = 1 } }); var controller = new StoresController(mockRepo.Object, null); // act IActionResult result = await controller.Index(); // assert // ...that the result is a ViewResult var viewResult = Assert.IsAssignableFrom <ViewResult>(result); // ...that the model of the view is a List<Store> var list = Assert.IsAssignableFrom <List <Store> >(viewResult.Model); // ...that the list has one element with ID 1 (based on the MockRepo's data) Store store = Assert.Single(list); Assert.Equal(1, store.StoreId); // we might also test that the correct view was chosen (DailyTasks/Index) mockRepo.Verify(x => x.StoreRepository.All(), Times.Once); // verify that the method was called once }
public void Index_GetStores() { using (var context = new ApplicationDbContext(options)) { var dataProtectionProvider = new EphemeralDataProtectionProvider(); storeController = new StoresController(context, dataProtectionProvider, mockHttpContextAccessor.Object, mockEnvironment.Object); var resultTask = storeController.Index(); resultTask.Wait(); var model = (List <Store>)((ViewResult)resultTask.Result).Model; Assert.AreEqual(1, model.Count); } }
public async Task Index_DisplayStores() { // ARRANGE var mockRepository = new Mock <IRepository>(); // create a moq that returns store mockRepository.Setup(r => r.GetAllStoresAsync()).ReturnsAsync(GetTestSessions()); // make a using my mock var controller = new StoresController(mockRepository.Object); // ACT var result = await controller.Index(""); // assert var viewResult = Assert.IsType <ViewResult>(result); }
public async Task IndexShouldReturnViewWithData() { // arrange var mockRepo = new Mock <IUnitOfWork>(); // Moq gives us Mock class // which can implement interfaces at runtime // 1. you create the Mock // 2. you Setup the mock (giving it behavior) // 3. you give mock.Object to your test subject object // (4. you can use methods like Verify ot make sure that things were called on the mock object) mockRepo.Setup(x => x.StoreRepository.All()) .ReturnsAsync(new List <Store> { new Store { StoreId = 1 } }); // mockRepo.Setup(x => x.Delete(It.IsAny<int>())) // mockRepo.Setup(x => x.Delete(It.IsInRange<int>(-99999, 0, Moq.Range.Inclusive))) // mockRepo.Setup(x => x.Delete(5)) // .ThrowsAsync(new InvalidOperationException()); var controller = new StoresController(mockRepo.Object, null); // act IActionResult result = await controller.Index(); // assert // ...that the result is a ViewResult var viewResult = Assert.IsAssignableFrom <ViewResult>(result); // ...that the model of the view is a List<Store> var list = Assert.IsAssignableFrom <List <Store> >(viewResult.Model); // ...that the list has one element with ID 1 (based on the MockRepo's data) Store store = Assert.Single(list); Assert.Equal(1, store.StoreId); // we might also test that the correct view was chosen (DailyTasks/Index) mockRepo.Verify(x => x.StoreRepository.All(), Times.Once); // verify that the method was called once }
public void ReturnIndexViewWithStoresList_WithActionIndex() { // Arrange IEnumerable <Store> storeList = new List <Store> { new Store { Id = 1, Name = "Auchan" } }; Mock <IRepository <Store> > mockRepo = new Mock <IRepository <Store> >(); mockRepo.Setup(repo => repo.FindAll()).Returns(storeList); StoresController sut = new StoresController(mockRepo.Object); // Act var result = sut.Index(); // Assert var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsAssignableFrom <IEnumerable <Store> >(viewResult.Model); Assert.Equal(storeList.Count(), model.Count()); }
public void Index_ReturnsCorrectView_True() { Assert.IsInstanceOfType(_controller.Index(), typeof(ViewResult)); }