public void Delete_GetRequest_ReturnViewResultWithProduct()
        {
            var mockProductService         = new Mock <IProductService>();
            var mockCategoryService        = new Mock <ICategoryService>();
            var mockCategoryProductService = new Mock <ICategoryAndProductService>();

            var controller = new ProductsController(mockProductService.Object,
                                                    mockCategoryService.Object,
                                                    mockCategoryProductService.Object);

            mockProductService.Setup(x => x.GetProductById(3)).Returns(GetProducts().Single(x => x.Id == 3));
            mockCategoryService.Setup(x => x.GetAllCategories()).Returns(GetCategories());

            var result  = controller.Delete(3) as ViewResult;
            var product = result.ViewData.Model as Product;

            Assert.IsNotNull(product);
            Assert.AreEqual(3, product.Id);
            Assert.AreEqual("Test Product 3", product.Name);
            Assert.AreEqual("Test Description 3", product.Description);
            Assert.AreEqual(4, product.CategoryId);
        }
        public void DeleteProduct_OnlyDeleteIfFound()
        {
            //arrange
            var mockContext = new Mock <IBookStoreAppContext>();

            mockContext.Setup(x => x.Get(3)).Returns(GetDemoProduct());

            // act
            // sut (system under test)
            var sut = new ProductsController(mockContext.Object);

            // assert

            // deleting an object should return an OK result
            var result = sut.Delete(3);

            Assert.IsInstanceOfType(result, typeof(OkResult));

            // note: this test could be more thorough to test for attempting to delete an object that doesn't exist
            // but because we're using a fake list in the API, this was not implemented, but its something that would make
            // test more complete.
        }
Esempio n. 3
0
        public async Task ProductsController_Delete_Success()
        {
            var createRequest = new ProductCreateRequest {
                Name = "Test product6"
            };

            var controller   = new ProductsController(_fixture.ProductService);
            var createResult = await controller.Post(createRequest);

            var createResponse = GetResponse <ProductCreateResponse>(createResult);

            Assert.Equal("Test product6", createResponse.Name);

            var deleteResult = await controller.Delete(createResponse.Id);

            Assert.IsType <OkResult>(deleteResult);

            var getResult = await controller.Get(createResponse.Id);

            var getOkResult = Assert.IsType <OkObjectResult>(getResult);

            Assert.Null(getOkResult.Value);
        }
Esempio n. 4
0
        public void DeleteProduct()
        {
            var controller = new ProductsController();

            controller.Request       = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            //Act
            var response = controller.Delete(1);

            //Check product has been deleted successfully
            Assert.IsTrue(response);

            //Add the deleted item back
            controller.Put(new Product
            {
                Category      = Category.Camera,
                Name          = "Nikon",
                Price         = 50000,
                Description   = "Professional Camera",
                Specification = "23 MP",
            });
        }
        public async Task Delete_GivenValidId_DeletesExistingProductAsync()
        {
            var options = new DbContextOptionsBuilder <ApiContext>()
                          .UseInMemoryDatabase("Delete_GivenValidId_DeletesExistingProduct")
                          .Options;

            using (var context = new ApiContext(options))
            {
                context.Products.Add(new Product {
                    Id = "123", Description = "Description 1", Model = "Model 1", Brand = "Brand 1"
                });
                context.SaveChanges();
            }

            using (var context = new ApiContext(options))
            {
                var controller = new ProductsController(context);
                var result     = await controller.Delete("123");

                Assert.IsType <NoContentResult>(result);
                Assert.Empty(context.Products);
            }
        }
        public void DeleteValidProductsTest()
        {
            Guid    productID = Guid.NewGuid();
            Product prod      = new Product {
                ABCID = productID, Title = "Product 1"
            };

            Mock <IProductRepository> mockRepository = new Mock <IProductRepository>();

            mockRepository.Setup(m => m.Products).Returns(new Product[] {
                prod,
                new Product {
                    ABCID = Guid.NewGuid(), Title = "Product 2"
                },
                new Product {
                    ABCID = Guid.NewGuid(), Title = "Product 3"
                },
            }.AsQueryable());

            Mock <IUserPreferenceService> mockUserPrefService = new Mock <IUserPreferenceService>();

            mockUserPrefService.Setup(prefService => prefService.Preferences).Returns(new UserPreferenceInfo()
            {
                ProductsPerPage         = 5,
                ProductColumnsToDisplay = new List <string>()
            });

            Mock <ILocalizedMessageService> mockMessageService = new Mock <ILocalizedMessageService>();

            mockMessageService.Setup(messageService => messageService.ProductSaved).Returns("{0} was deleted.");

            ProductsController controller = new ProductsController(mockRepository.Object, mockUserPrefService.Object, mockMessageService.Object);

            controller.Delete(prod.ABCID);

            mockRepository.Verify(m => m.DeleteProduct(prod.ABCID));
        }
Esempio n. 7
0
        //public void Delete(int id)
        // Method not implemented in Controller - refactor test once it is written
        public void Delete_ShouldReturnNull()
        {
            // arrange
            var     productId = 5;
            Product product   = new Product()
            {
                ProductId   = productId,
                Description = "Test product",
                ProductName = "Hammer",
                Price       = 20,
                ProductCode = "ABC-9876",
                ReleaseDate = DateTime.Now
            };
            var productsController = new ProductsController(ProductRepository.Object);
            //ProductRepository.Setup(p => p.Save(productId, product)).Returns(product);

            // act
            IHttpActionResult actionResult = productsController.Delete(productId);
            var contentResult = actionResult as OkResult;

            // assert
            Assert.IsNotNull(contentResult);
            Assert.IsInstanceOfType(actionResult, typeof(OkResult));
        }
        public void Delete()
        {
            //Arrange
            var productId = Guid.NewGuid();
            var product   = new Product
            {
                Id            = productId,
                Name          = "Dummy Name 1",
                Description   = "Dummy Description 1",
                Price         = 123.45M,
                DeliveryPrice = 67.89M
            };

            _productsController.Create(product);

            _productIds.Add(productId);

            //Act
            _productsController.Delete(productId);

            //Assert
            _productsController.Invoking(controller => controller.GetProduct(productId))
            .ShouldThrow <HttpResponseException>();
        }
Esempio n. 9
0
        public async void Delete_IdIsNull_ReturnNotFound()
        {
            var result = await _controller.Delete(null);

            var redirect = Assert.IsType <NotFoundResult>(result);
        }
        public void DeleteTest()
        {
            var result = _productsController.Delete(1);

            Assert.IsType <OkObjectResult>(result);
        }
        public void Delete_ShouldReturnProductWithSameId()
        {
            // Arrange
            var item = Helpers.CreateTestProduct();

            var mockRepository = new Mock<IProductRepository>();
            mockRepository.Setup(x => x.GetProduct(item.Id)).Returns(item);
            mockRepository.Setup(x => x.DeleteProduct(item));

            var controller = new ProductsController(mockRepository.Object);

            // Act
            IHttpActionResult actionResult = controller.Delete(item.Id);
            var contentResult = actionResult as OkNegotiatedContentResult<Product>;

            // Assert
            Assert.IsNotNull(contentResult);
            Assert.IsNotNull(contentResult.Content);
            Assert.AreEqual(item.Id, contentResult.Content.Id);
        }       
        public void Delete_ShouldReturnOkResult()
        {
            // Arrange
            var item = Helpers.CreateTestProduct();

            var mockRepository = new Mock<IProductRepository>();
            mockRepository.Setup(x => x.GetProduct(item.Id)).Returns(item);
            //mockRepository.Setup(x => x.DeleteProduct(item.Id));

            var controller = new ProductsController(mockRepository.Object);

            // Act
            IHttpActionResult actionResult = controller.Delete(item.Id);

            // Assert
            Assert.IsInstanceOfType(actionResult, typeof(OkNegotiatedContentResult<Product>));
        }
        public void Delete_ShouldReturnNotFoundWhenProductDoesNotExist()
        {
            // Arrange
            var item = Helpers.CreateTestProduct();

            var mockRepository = new Mock<IProductRepository>();
            mockRepository.Setup(x => x.GetProduct(item.Id));
            var controller = new ProductsController(mockRepository.Object);

            // Act
            IHttpActionResult actionResult = controller.Delete(item.Id);

            // Assert
            Assert.IsInstanceOfType(actionResult, typeof(NotFoundResult));
        }
        public async void Should_DeleteAction_ReturnsNotFound_If_Id_IsNull()
        {
            var result = await controller.Delete(null);

            Assert.IsType <NotFoundResult>(result);
        }
Esempio n. 15
0
        public async Task DeleteProductTest()
        {
            //Arrange
            var productId = 33;
            var userId    = 11;
            List <UserProduct> products = new List <UserProduct>
            {
                new UserProduct {
                    Checked = true, ProductId = 33, UserId = 11
                },
                new UserProduct {
                    Checked = true, ProductId = 33, UserId = 14
                },
                new UserProduct {
                    Checked = true, ProductId = 33, UserId = 17
                }
            };

            List <ProvidersProductInfo> providersProductInfos = new List <ProvidersProductInfo>
            {
                new ProvidersProductInfo {
                    ImageUrl = "asd", MinPrice = 12, MaxPrice = 16, ProviderName = "Onliner", Id = 1, Url = "qwe"
                }
            };

            var product = new Product
            {
                Id = productId,
                ExternalProductId = "12345",
                Name = "asdasasf",
                ProvidersProductInfos = providersProductInfos,
                UserProducts          = products
            };

            var mockProductService        = new Mock <IProductService>();
            var mockUserService           = new Mock <IUserService>();
            var mockProductMessageService = new Mock <IProductMessageService>();
            var mockElasticService        = new Mock <IElasticService <Product> >();

            mockProductService.Setup(x => x.GetById(productId))
            .ReturnsAsync(new Product
            {
                Id = productId,
                ExternalProductId = "12345",
                Name = "asdasasf",
                ProvidersProductInfos = providersProductInfos,
                UserProducts          = products
            }).Verifiable();

            mockProductService.Setup(x => x.DeleteFromUserProduct(userId, productId)).Returns(Task.FromResult(false)).Verifiable();
            mockElasticService.Setup(x => x.DeleteFromIndex(It.IsAny <int>())).Verifiable();
            mockUserService.Setup(x => x.GetById(userId))
            .ReturnsAsync(new User
            {
                Id = userId,
                SocialNetworkName   = "Twitter",
                Username            = "******",
                SocialNetworkUserId = "297397558",
                Token        = "4f60b211517aa86a67bace12231d2530",
                Email        = "*****@*****.**",
                UserProducts = products
            }).Verifiable();

            mockProductService.Setup(x => x.Delete(product));
            var controller = new ProductsController(mockProductService.Object, mockUserService.Object, mockElasticService.Object, mockProductMessageService.Object)
            {
                Request = new HttpRequestMessage()
            };

            //Set up OwinContext
            controller.Request.SetOwinContext(new OwinContext());
            var owinContext = controller.Request.GetOwinContext();

            owinContext.Set("userId", userId);

            //Act
            IHttpActionResult result = await controller.Delete(productId);

            //Assert
            Assert.IsInstanceOfType(result, typeof(OkResult));
            mockProductService.Verify();
        }
Esempio n. 16
0
        public void TestDeleteProductOk()
        {
            var result = _prodController.Delete("4");

            Assert.IsTrue(result is OkResult);
        }
        public void DeleteProduct()
        {
            var productController  = new ProductsController(this._productRepositoryMock.Object);
            var productCreateModel = TestHelper.GetProductModel();

            productController.Create(productCreateModel);
            this._productRepositoryMock.Verify(x => x.Add(productCreateModel), Times.Once);
            this._productRepositoryMock.Setup(x => x.Find(productCreateModel.Name)).Returns(productCreateModel);

            System.Web.Http.Results.OkNegotiatedContentResult <ProductsApi.Models.Product> result = (System.Web.Http.Results.OkNegotiatedContentResult <ProductsApi.Models.Product>)productController.Delete(productCreateModel.Name);

            this._productRepositoryMock.Verify(x => x.Remove(productCreateModel.Name), Times.Once);
            Assert.AreSame(productCreateModel, result.Content);
        }