コード例 #1
0
 public void DeleteProduct(Product product)
 {
     _context.Products.Remove(product);
     _context.SaveChanges();
 }
コード例 #2
0
 public void ChangeProduct(Product product)
 {           
     _context.MarkAsModified(product);
     _context.SaveChanges();
 }
コード例 #3
0
 public void InsertProduct(Product product)
 {
     _context.Products.Add(product);
     _context.SaveChanges();
 }
        public void Post_ShouldAddProductToRepo()
        {
            // Arrange
            var mockRepository = new Mock<IProductRepository>();
            ProductsController controller = new ProductsController(mockRepository.Object);

            controller.Request = new HttpRequestMessage
            {
                RequestUri = new Uri(string.Format("http://{0}:{1}/api/inventory/products", "localhost", 65217))
            };

            controller.Configuration = new HttpConfiguration();
            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/inventory/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional });

            controller.RequestContext.RouteData = new HttpRouteData(
                route: new HttpRoute(),
                values: new HttpRouteValueDictionary { { "controller", "products" } });

            // Act
            Product product = new Product() { Id = "MyTitle", Title = "Product1" };
            var actionResult = controller.Post(product);

            // Assert           
            mockRepository.Verify(m => m.InsertProduct(It.Is<Product>(arg => arg.Id == "MyTitle")));
        }
        public void Post_ShouldSetRouteValues()
        {
            // Arrange
            var mockRepository = new Mock<IProductRepository>();                  
            ProductsController controller = new ProductsController(mockRepository.Object);

            controller.Request = new HttpRequestMessage
            {
                RequestUri = new Uri(string.Format("http://{0}:{1}/api/inventory/products", "localhost", 65217))
            };

            controller.Configuration = new HttpConfiguration();
            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/inventory/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional });

            controller.RequestContext.RouteData = new HttpRouteData(
                route: new HttpRoute(),
                values: new HttpRouteValueDictionary { { "controller", "products" } });

            // Act
            Product product = new Product() { Id = "MyTitle", Title = "Product1" };
            var actionResult = controller.Post(product);

            // Assert
            var response = actionResult as CreatedAtRouteNegotiatedContentResult<Product>;

            // Implementation uses WebAPI helper class to ensure that the response headers are set correct.
            // Only need to test that the ROute VAlues are configured
            Assert.IsNotNull(response);
            Assert.AreEqual("DefaultApi", response.RouteName);
            Assert.AreEqual("MyTitle", response.RouteValues["Id"]);

            mockRepository.Verify(m => m.InsertProduct(It.Is<Product>(arg => arg.Id == "MyTitle")));
        }
コード例 #6
0
        public void MarkAsModified(Product item)        {

            Entry(item).State = EntityState.Modified;
        }