public void GetProductById_WhenNotExist_ThrowException() { // Arrange var mockRepository = new Mock <IProductRepository>(); Guid testId = Guid.NewGuid(); mockRepository.Setup(p => p.GetProductById(testId)) .Throws <Exception>(); var service = new ProductDomainService(mockRepository.Object); // Act & Assert Assert.Throws <Exception>(() => service.GetProductById(testId)); }
/// <summary> /// Gets the product cost (one item) /// </summary> /// <param name="product">Product</param> /// <param name="attributesXml">Shopping cart item attributes in XML</param> /// <returns>Product cost (one item)</returns> public virtual decimal GetProductCost(Product product, string attributesXml) { if (product == null) { throw new ArgumentNullException("product"); } decimal cost = product.ProductCost; var attributeValues = _productAttributeParser.ParseProductAttributeValues(attributesXml); foreach (var attributeValue in attributeValues) { switch (attributeValue.AttributeValueType) { case AttributeValueType.Simple: { //simple attribute cost += attributeValue.Cost; } break; case AttributeValueType.AssociatedToProduct: { //bundled product var associatedProduct = _productService.GetProductById(attributeValue.AssociatedProductId); if (associatedProduct != null) { cost += associatedProduct.ProductCost * attributeValue.Quantity; } } break; default: break; } } return(cost); }
public void GetProductById_WhenExist() { // Arrange var mockRepository = new Mock <IProductRepository>(); Guid testId = Guid.NewGuid(); mockRepository.Setup(p => p.GetProductById(testId)) .Returns(new Product() { Id = testId, Name = "Guasha" }); var services = new ProductDomainService(mockRepository.Object); // Act var check = services.GetProductById(testId); // Assert var result = Assert.IsType <Product>(check); Assert.Equal(testId, result.Id); }
public ProductDetailsOutput ProductDetails(ProductDetailsInput input) { var product = _productDomainService.GetProductById(input.ProductId.GetValueOrDefault()); if (product == null || product.Deleted) { throw new UserFriendlyException(string.Format("商品[ID:{0}]不存在", input.ProductId.GetValueOrDefault())); } //published? //if (!_catalogSettings.AllowViewUnpublishedProductPage) //{ // //Check whether the current user has a "Manage catalog" permission // //It allows him to preview a product before publishing // if (!product.Published && !_permissionService.Authorize(StandardPermissionProvider.ManageProducts)) // return InvokeHttp404(); //} ////ACL (access control list) //if (!_aclService.Authorize(product)) // return InvokeHttp404(); ////Store mapping //if (!_storeMappingService.Authorize(product)) // return InvokeHttp404(); ////availability dates //if (!product.IsAvailable()) // return InvokeHttp404(); //visible individually? //if (!product.VisibleIndividually) //{ // //is this one an associated products? // var parentGroupedProduct = _productService.GetProductById(product.ParentGroupedProductId); // if (parentGroupedProduct == null) // return RedirectToRoute("HomePage"); // return RedirectToRoute("Product", new { SeName = parentGroupedProduct.GetSeName() }); //} //update existing shopping cart item? ShoppingCartItem updatecartitem = null; //if (_shoppingCartSettings.AllowCartItemEditing && updatecartitemid > 0) //{ // var cart = CurrentUser.ShoppingCartItems // .Where(x => x.ShoppingCartType == ShoppingCartType.ShoppingCart) // .LimitPerStore(_storeContext.CurrentStore.Id) // .ToList(); // updatecartitem = cart.FirstOrDefault(x => x.Id == updatecartitemid); // //not found? // if (updatecartitem == null) // { // return RedirectToRoute("Product", new { SeName = product.GetSeName() }); // } // //is it this product? // if (product.Id != updatecartitem.ProductId) // { // return RedirectToRoute("Product", new { SeName = product.GetSeName() }); // } //} //prepare the model var model = PrepareProductDetailsPageDto(product, updatecartitem, false); ////save as recently viewed //_recentlyViewedProductsService.AddProductToRecentlyViewedList(product.Id); ////activity log //_customerActivityService.InsertActivity("PublicStore.ViewProduct", _localizationService.GetResource("ActivityLog.PublicStore.ViewProduct"), product.Name); //return View(model.ProductTemplateViewPath, model); return(new ProductDetailsOutput() { ProductDetail = model }); }