Ejemplo n.º 1
0
        public async Task EditGetAction_UpdateModelFromService()
        {
            // Arrange

            // Act
            var result = await validController.Edit(entity.Id);

            // Assert
            mock.Verify(a => a.GetAsync(entity.Id, true));
        }
Ejemplo n.º 2
0
        public void EdittemSuccessUpdateMockDb()
        {
            var id            = Guid.NewGuid();
            var existing_item = new Item {
                item_id = id, name = "item4", price = 40.00M, quantity = 20
            };
            var updated_item = new Item {
                item_id = id, name = "item4", price = 50.00M, quantity = 20
            };


            var vm       = new ItemViewModel(updated_item);
            var mockItem = new Mock <DbSet <Item> >();

            mockItem.Setup(m => m.Find(id)).Returns(existing_item);

            var mockContext = new Mock <StoreDbContext>();

            mockContext.Setup(m => m.Items).Returns(mockItem.Object);

            var controller = new ItemsController(mockContext.Object);

            var result = controller.Edit(vm);

            Assert.AreEqual(mockContext.Object.Items.Find(id).price, 50.00M);
        }
        public void test_Db_Edit_UpdatesInDb()
        {
            //Arrrange
            ItemsController controller = new ItemsController(db);
            Item            testItem   = new Item
            {
                ItemId      = 16,
                Name        = "Lil Pump",
                Description = "rap gummi",
                Cost        = 234
            };
            Item updatedItem = new Item
            {
                ItemId      = 9,
                Name        = "Lil Flip",
                Description = "rap dummi",
                Cost        = 123
            };

            //Act
            controller.Create(testItem);
            testItem.Name = "Lil Flip";
            controller.Edit(testItem);
            var returnItem = (controller.Details(1) as ViewResult).ViewData.Model as Item;

            //Assert
            Assert.AreEqual(returnItem.Name, "Lil Flip");
        }
Ejemplo n.º 4
0
        public async Task EditPostAction_ModelError_ReturnsSameModel()
        {
            // Arrange

            // Act
            var result = await notValidController.Edit(entity.Id, entity) as ViewResult;

            // Assert
            Assert.Equal(entity, result.Model);
        }
Ejemplo n.º 5
0
        public void Get_ViewResult_Edit_Test()
        {
            //Arrange
            ItemsController controller = new ItemsController();

            //Act
            var result = controller.Edit(0);

            //Assert
            Assert.IsType <ViewResult>(result);
        }
        public void Edit_get_no_id_provided_throws_not_found(string value)
        {
            //+ Arrange
            ItemsController controller = new ItemsController(_fakeLogger, _itemService, _selectListService);

            //+ Act
            var actionResult = controller.Edit(value);

            //+ Assert
            Assert.IsType <NotFoundResult>(actionResult);
        }
Ejemplo n.º 7
0
        public async Task Post_Edit_Item_Success_User_Admin_Returns_Item_Not_Approved_AndRedirectResult()
        {
            //Arrange
            var httpContextMock = new Mock <HttpContextBase>();

            var mockPrincipal = new Mock <IPrincipal>();

            mockPrincipal.Setup(x => x.Identity.IsAuthenticated).Returns(true);
            mockPrincipal.Setup(x => x.IsInRole(It.IsAny <string>())).Returns(false);
            httpContextMock.Setup(m => m.User).Returns(mockPrincipal.Object);

            //Mock a file download
            var request = new Mock <HttpPostedFileBase>();
            var postedfilesKeyCollection = new Mock <HttpFileCollectionBase>();

            httpContextMock.SetupGet(x => x.Request.Files[It.IsAny <string>()]).Returns(request.Object);
            Item item = new Item {
                ID = 1, UserID = "john123", ItemType = ItemType.ap
            };

            var repository = new Mock <IItemsRepository>();

            repository.Setup(repo => repo.UpdateItem(It.IsAny <Item>()))
            .Returns(Task.CompletedTask)
            .Verifiable();

            var controller = new ItemsController(repository.Object)
            {
                ControllerContext = new ControllerContext
                {
                    HttpContext = httpContextMock.Object
                }
            };

            //Act
            var result = await controller.Edit(item, "", "");

            //Assert
            Assert.False(item.Approved);
            var RedirectToRouteResult = Assert.IsType <RedirectToRouteResult>(result);

            Assert.Equal("Index", RedirectToRouteResult.RouteValues["action"]);
            Assert.Equal(ItemType.ap, RedirectToRouteResult.RouteValues["ItemType"]);
            repository.Verify(); //Only Verifies update was actually called.
        }
Ejemplo n.º 8
0
        public async Task Get_Edit_Null_Item_ReturnsHttpNotFound()
        {
            //Arrange
            var  mock = new Mock <IItemsRepository>();
            Item item = null;

            mock.Setup(x => x.GetItem(It.IsAny <int>())).ReturnsAsync(item);
            var controller = new ItemsController(mock.Object);
            int id         = 1;

            //Act
            ActionResult result = await controller.Edit(id);

            //Assert
            var viewResult = Assert.IsType <HttpNotFoundResult>(result);

            mock.Verify();
        }
        public void Mock_PostResultViewEdit_Viewresult()
        {
            //Arrange
            Item testItem = new Item();

            testItem.ItemId      = 2;
            testItem.Name        = "gorilla glue";
            testItem.Cost        = 3;
            testItem.Description = "dankest gummi bear";
            DbSetup();
            ItemsController controller = new ItemsController(mock.Object);

            //Act
            var resultView = controller.Edit(testItem.ItemId) as ViewResult;

            //Assert
            Assert.IsInstanceOfType(resultView, typeof(ViewResult));
        }
Ejemplo n.º 10
0
        public async Task Edit_ReturnsViewResultWithItemModel()
        {
            // Arrange
            var Item = new Item()
            {
                Id = 1
            };
            var mockRepo = new Mock <IInvoicesRepository>();

            mockRepo.Setup(repo => repo.GetItemAsync(Item.Id)).ReturnsAsync(Item);
            var controller = new ItemsController(mockRepo.Object);

            // Act
            var result = await controller.Edit(1);

            // Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <Item>(viewResult.ViewData.Model);

            Assert.Equal(Item.Id, model.Id);
        }
Ejemplo n.º 11
0
        public async Task Post_Edit_Item_Fails_User_Admin_Returns_Item_Approved_AndActionResult()
        {
            //Arrange
            var httpContextMock = new Mock <HttpContextBase>();

            var mockPrincipal = new Mock <IPrincipal>();

            mockPrincipal.Setup(x => x.Identity.IsAuthenticated).Returns(true);
            mockPrincipal.Setup(x => x.IsInRole(It.IsAny <string>())).Returns(true);
            httpContextMock.Setup(m => m.User).Returns(mockPrincipal.Object);

            //Mock a file download
            var request = new Mock <HttpPostedFileBase>();
            var postedfilesKeyCollection = new Mock <HttpFileCollectionBase>();

            httpContextMock.SetupGet(x => x.Request.Files[It.IsAny <string>()]).Returns(request.Object);
            Item item = new Item {
                ID = 1, UserID = "john123",
            };

            var repository = new Mock <IItemsRepository>();

            var controller = new ItemsController(repository.Object)
            {
                ControllerContext = new ControllerContext
                {
                    HttpContext = httpContextMock.Object
                }
            };

            controller.ModelState.AddModelError("title", "title is required");

            //Act
            var result = await controller.Edit(item, "", "");

            //Assert
            Assert.True(item.Approved);
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsType <Item>(viewResult.ViewData.Model);
        }
Ejemplo n.º 12
0
        public async Task Get_Edit_Item_ReturnsViewResult()
        {
            //Arrange
            var  mock = new Mock <IItemsRepository>();
            Item item = new Item {
                ID = 1, UserID = "john123", Title = "New Title"
            };

            mock.Setup(x => x.GetItem(It.IsAny <int>())).ReturnsAsync(item);
            var controller = new ItemsController(mock.Object);

            //Act
            ActionResult result = await controller.Edit(item.ID);

            //Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsType <Item>(viewResult.ViewData.Model);

            Assert.Equal(item.ID, model.ID);
            Assert.Equal(item.UserID, model.UserID);
            Assert.Equal(item.Title, model.Title);
        }
Ejemplo n.º 13
0
        public async Task Edit_RedirectsToInvoicesDetails()
        {
            var item = new Item()
            {
                Id = 1
            };
            // Arrange
            var mockRepo = new Mock <IInvoicesRepository>();

            mockRepo.Setup(repo => repo.UpdateItemAsync(item))
            .ReturnsAsync(item);
            var controller = new ItemsController(mockRepo.Object);

            // Act
            var result = await controller.Edit(1, item);

            // Assert
            var redirect = Assert.IsType <RedirectToActionResult>(result);

            Assert.Equal("Details", redirect.ActionName);
            Assert.Equal("Invoices", redirect.ControllerName);
        }
Ejemplo n.º 14
0
        public void EdittemSuccessViewRenders()
        {
            var id            = Guid.NewGuid();
            var existing_item = new Item {
                item_id = id, name = "item4", price = 40.00M, quantity = 30
            };
            //var updated_item = new Item { item_id = id , name = "item4", price = 50.00M };

            var mockItem = new Mock <DbSet <Item> >();

            mockItem.Setup(m => m.Find(id)).Returns(existing_item);

            var mockContext = new Mock <StoreDbContext>();

            mockContext.Setup(m => m.Items).Returns(mockItem.Object);

            var controller = new ItemsController(mockContext.Object);

            var result = controller.Edit(id);

            Assert.IsAssignableFrom(typeof(ViewResult), result);
        }
Ejemplo n.º 15
0
        public void EdittemInvalidIdThrowException()
        {
            var id            = Guid.NewGuid();
            var existing_item = new Item {
                item_id = id, name = "item4", price = 40.00M, quantity = 50
            };
            //var updated_item = new Item { item_id = id , name = "item4", price = 50.00M };

            var mockItem = new Mock <DbSet <Item> >();

            mockItem.Setup(m => m.Find(id)).Returns(existing_item);

            var mockContext = new Mock <StoreDbContext>();

            mockContext.Setup(m => m.Items).Returns(mockItem.Object);

            var controller = new ItemsController(mockContext.Object);

            var result = controller.Edit(Guid.NewGuid());

            Assert.IsAssignableFrom(typeof(HttpNotFoundResult), result);
        }