Пример #1
0
        public void DeleteProductCallsRepositoryRemove()
        {
            //// Arrange
            Guid removedKey = Guid.Empty;

            Guid productKey = new Guid();
            ProductActual productActual = CreateFakeProduct(productKey, 20.0M);

            var MockProductService = new Mock<IProductService>();
            MockProductService.Setup(cs => cs.Delete(productActual, It.IsAny<bool>())).Callback<IProductActual, bool>((p, b) => removedKey = p.Key);
            MockProductService.Setup(cs => cs.GetByKey(productKey)).Returns(productActual);

            MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object);

            ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext);
            ctrl.Request = new HttpRequestMessage();
            ctrl.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());

            //// Act
            HttpResponseMessage response = ctrl.Delete(productKey);

            //// Assert
            Assert.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode);

            Assert.AreEqual(productKey, removedKey);
        }
Пример #2
0
        public void GetProductByKeyReturnsCorrectItemFromRepository()
        {
            //// Arrange
            Guid productKey = Guid.NewGuid();
            ProductActual productActual = MockProductDataMaker.MockProductComplete(productKey) as ProductActual;

            var MockProductService = new Mock<IProductService>();
            MockProductService.Setup(cs => cs.GetByKey(productKey)).Returns(productActual);

            MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object);

            ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext);

            //// Act
            var result = ctrl.GetProduct(productKey);

            //// Assert
            Assert.AreEqual(productActual, result);
        }
Пример #3
0
        public void PutProductUpdatesRepository()
        {
            //// Arrange
            bool wasCalled = false;
            Guid productKey = Guid.NewGuid();
            ProductActual productActual = CreateFakeProduct(productKey, 20.0M);

            var MockProductService = new Mock<IProductService>();
            MockProductService.Setup(cs => cs.Save(productActual, It.IsAny<bool>())).Callback(() => wasCalled = true);

            MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object);

            ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext);
            ctrl.Request = new HttpRequestMessage();
            ctrl.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());

            //// Act
            HttpResponseMessage response = ctrl.PutProduct(productActual);

            //// Assert
            Assert.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode);

            Assert.True(wasCalled);
        }
Пример #4
0
        public void PutProductReturns500WhenRepositoryUpdateReturnsError()
        {
            //// Arrange
            Guid productKey = Guid.NewGuid();
            ProductActual productActual = CreateFakeProduct(productKey, 20.0M);

            var MockProductService = new Mock<IProductService>();
            MockProductService.Setup(cs => cs.Save(productActual, It.IsAny<bool>())).Throws<InvalidOperationException>();

            MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object);

            ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext);
            ctrl.Request = new HttpRequestMessage();
            ctrl.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());

            //// Act
            HttpResponseMessage response = ctrl.PutProduct(productActual);

            //// Assert
            Assert.AreEqual(System.Net.HttpStatusCode.NotFound, response.StatusCode);
        }
Пример #5
0
        public void NewProductReturnsCorrectProduct()
        {
            //// Arrange
            bool wasCalled = false;
            Guid productKey = Guid.NewGuid();
            ProductActual productActual = CreateFakeProduct(productKey, 20.0M);

            var MockProductService = new Mock<IProductService>();
            MockProductService.Setup(cs => cs.CreateProduct(productActual.Sku, productActual.Name, productActual.Price)).Returns(productActual).Callback(() => wasCalled = true);

            MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object);

            ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext);

            //// Act
            ProductActual result = ctrl.NewProduct(productActual.Sku, productActual.Name, productActual.Price);

            //// Assert
            Assert.AreEqual(productActual, result);
            Assert.True(wasCalled);
        }
Пример #6
0
        public void GetProductThrowsWhenRepositoryReturnsNull()
        {
            //// Arrange
            Guid productKey = Guid.NewGuid();

            var MockProductService = new Mock<IProductService>();
            MockProductService.Setup(cs => cs.GetByKey(productKey)).Returns((ProductActual)null);

            MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object);

            ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext);

            //// Act & Assert
            var ex = Assert.Throws<HttpResponseException>(() => ctrl.GetProduct(Guid.Empty));
        }
Пример #7
0
        public void GetProductByKeysReturnsCorrectItemsFromRepository()
        {
            //// Arrange
            Guid productKey = Guid.NewGuid();
            ProductActual productActual = CreateFakeProduct(productKey, 20.0M);

            Guid productKey2 = Guid.NewGuid();
            ProductActual product2 = CreateFakeProduct(productKey2, 30.0M);

            Guid productKey3 = Guid.NewGuid();
            ProductActual product3 = CreateFakeProduct(productKey3, 40.0M);

            List<ProductActual> productsList = new List<ProductActual>();
            productsList.Add(productActual);
            productsList.Add(product3);

            var productKeys = new[] { productKey, productKey3 };

            var MockProductService = new Mock<IProductService>();
            MockProductService.Setup(cs => cs.GetByKeys(productKeys)).Returns(productsList);

            MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object);

            ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext);

            //// Act
            var result = ctrl.GetProducts(productKeys);

            //// Assert
            Assert.AreEqual(productsList, result);
        }