2015-03-03



TOTD stands for think of the developer and I feel its something that the commerce team should have as an objective. As a company, we are new to Episerver Commerce but it seems that we have to do way too much work to unit test our commerce code. This article will describe how we went about unit testing update quantity cart code. So lets look at the code to update the quantity of an item in the basket.

Ok, so we need to mock CartHelper, but its a static method, so we need to write a cart wrapper and inject that into the class we want to test.

You can see that we have to also wrap the line items in its own wrapper, so lets do that.

// Copyright 2015 Green Man Gaming
using System;
using System.Runtime.Serialization;

using Mediachase.Commerce.Orders;

namespace GMGShop.Domain.Wrappers
{
public class LineItemWrapper : ILineItemWrapper
{
private readonly LineItem lineItem;

/// <summary>
/// Initializes a new instance of the <see cref="LineItemWrapper"/> class.
/// </summary>
/// <param name="lineItem"></param>
public LineItemWrapper(LineItem lineItem)
{
this.lineItem = lineItem;
}

/// <summary>
/// Initializes a new instance of the <see cref="LineItemWrapper"/> class.
/// </summary>
public LineItemWrapper()
{
lineItem = new LineItem();
}

/// <summary>
/// Gets the list of discounts that were applied to that particular line item.
/// </summary>
/// <value>
/// The discounts.
/// </value>
public LineItemDiscountCollection Discounts
{
get
{
return lineItem.Discounts;
}
}

/// <summary>
/// Gets the parent Order Form.
/// </summary>
/// <value>
/// The parent.
/// </value>
public OrderForm Parent
{
get
{
return lineItem.Parent;
}
}

/// <summary>
/// Gets the line item id.
/// </summary>
/// <value>
/// The line item id.
/// </value>
public int LineItemId
{
get
{
return lineItem.LineItemId;
}
}

/// <summary>
/// Gets or sets the order form id.
/// </summary>
/// <value>
/// The order form id.
/// </value>
public int OrderFormId
{
get
{
return lineItem.OrderFormId;
}

set
{
lineItem.OrderFormId = value;
}
}

/// <summary>
/// Gets or sets the order group id.
/// </summary>
/// <value>
/// The order group id.
/// </value>
public int OrderGroupId
{
get
{
return lineItem.OrderGroupId;
}

set
{
lineItem.OrderGroupId = value;
}
}

/// <summary>
/// Gets or sets the catalog.
/// </summary>
/// <value>
/// The catalog.
/// </value>
public string Catalog
{
get
{
return lineItem.Catalog;
}

set
{
lineItem.Catalog = value;
}
}

/// <summary>
/// Gets or sets the catalog node.
/// </summary>
/// <value>
/// The catalog node.
/// </value>
public string CatalogNode
{
get
{
return lineItem.CatalogNode;
}

set
{
lineItem.CatalogNode = value;
}
}

/// <summary>
/// Gets or sets the parent catalog entry id. Typically for Product/Sku(Variation) types of products, this will be a product code.
/// </summary>
/// <value>
/// The parent catalog entry id.
/// </value>
public string ParentCatalogEntryId
{
get
{
return lineItem.ParentCatalogEntryId;
}

set
{
lineItem.ParentCatalogEntryId = value;
}
}

/// <summary>
/// Gets or sets the catalog entry code.
/// </summary>
/// <value>
/// The catalog entry id.
/// </value>
public string CatalogEntryId
{
get
{
return lineItem.CatalogEntryId;
}

set
{
lineItem.CatalogEntryId = value;
}
}

/// <summary>
/// Gets or sets the quantity.
/// </summary>
/// <value>
/// The quantity.
/// </value>
public Decimal Quantity
{
get
{
return lineItem.Quantity;
}

set
{
lineItem.Quantity = value;
}
}

/// <summary>
/// Gets or sets the min quantity.
/// </summary>
/// <value>
/// The min quantity.
/// </value>
public Decimal MinQuantity
{
get
{
return lineItem.MinQuantity;
}

set
{
lineItem.MinQuantity = value;
}
}

/// <summary>
/// Gets or sets the max quantity.
/// </summary>
/// <value>
/// The max quantity.
/// </value>
public Decimal MaxQuantity
{
get
{
return lineItem.MaxQuantity;
}

set
{
lineItem.MaxQuantity = value;
}
}

/// <summary>
/// Gets or sets the placed price.
/// </summary>
/// <value>
/// The placed price.
/// </value>
public Decimal PlacedPrice
{
get
{
return lineItem.PlacedPrice;
}

set
{
lineItem.PlacedPrice = value;
}
}

/// <summary>
/// Gets or sets the list price. The price that the item is listed in the catalog.
/// </summary>
/// <value>
/// The list price.
/// </value>
public Decimal ListPrice
{
get
{
return lineItem.ListPrice;
}

set
{
lineItem.ListPrice = value;
}
}

/// <summary>
/// Gets or sets the line item discount amount.
/// </summary>
/// <value>
/// The line item discount amount.
/// </value>
public Decimal LineItemDiscountAmount
{
get
{
return lineItem.LineItemDiscountAmount;
}

set
{
lineItem.LineItemDiscountAmount = value;
}
}

/// <summary>
/// Gets or sets the order level discount amount.
/// </summary>
/// <value>
/// The order level discount amount.
/// </value>
public Decimal OrderLevelDiscountAmount
{
get
{
return lineItem.OrderLevelDiscountAmount;
}

set
{
lineItem.OrderLevelDiscountAmount = value;
}
}

/// <summary>
/// Gets or sets the shipping address name.
/// </summary>
/// <value>
/// The shipping address id.
/// </value>
public string ShippingAddressId
{
get
{
return lineItem.ShippingAddressId;
}

set
{
lineItem.ShippingAddressId = value;
}
}

/// <summary>
/// Gets or sets the name of the shipping method.
/// </summary>
/// <value>
/// The name of the shipping method.
/// </value>
public string ShippingMethodName
{
get
{
return lineItem.ShippingMethodName;
}

set
{
lineItem.ShippingMethodName = value;
}
}

/// <summary>
/// Gets or sets the shipping method id.
/// </summary>
/// <value>
/// The shipping method id.
/// </value>
public Guid ShippingMethodId
{
get
{
return lineItem.ShippingMethodId;
}

set
{
lineItem.ShippingMethodId = value;
}
}

/// <summary>
/// Gets or sets the extended price.
/// </summary>
/// <value>
/// The extended price.
/// </value>
public Decimal ExtendedPrice
{
get
{
return lineItem.ExtendedPrice;
}

set
{
lineItem.ExtendedPrice = value;
}
}

/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
public string Description
{
get
{
return lineItem.Description;
}

set
{
lineItem.Description = value;
}
}

/// <summary>
/// Gets or sets the status.
/// </summary>
/// <value>
/// The status.
/// </value>
public string Status
{
get
{
return lineItem.Status;
}

set
{
lineItem.Status = value;
}
}

/// <summary>
/// Gets or sets the name of the display.
/// </summary>
/// <value>
/// The name of the display.
/// </value>
public string DisplayName
{
get
{
return lineItem.DisplayName;
}

set
{
lineItem.DisplayName = value;
}
}

/// <summary>
/// Gets or sets a value indicating whether [allow backorders and preorders]
/// </summary>
/// <value>
/// <c>true</c> if [allow backorders and preorders]; otherwise, <c>false</c>.
/// </value>
public bool AllowBackordersAndPreorders
{
get
{
return lineItem.AllowBackordersAndPreorders;
}

set
{
lineItem.AllowBackordersAndPreorders = value;
}
}

/// <summary>
/// Gets or sets the in stock quantity.
/// </summary>
/// <value>
/// The in stock quantity.
/// </value>
public Decimal InStockQuantity
{
get
{
return lineItem.InStockQuantity;
}

set
{
lineItem.InStockQuantity = value;
}
}

/// <summary>
/// Gets or sets the preorder quantity.
/// </summary>
/// <value>
/// The preorder quantity.
/// </value>
public Decimal PreorderQuantity
{
get
{
return lineItem.PreorderQuantity;
}

set
{
lineItem.PreorderQuantity = value;
}
}

/// <summary>
/// Gets or sets the backorder quantity.
/// </summary>
/// <value>
/// The backorder quantity.
/// </value>
public Decimal BackorderQuantity
{
get
{
return lineItem.BackorderQuantity;
}

set
{
lineItem.BackorderQuantity = value;
}
}

/// <summary>
/// Gets or sets the inventory status.
/// </summary>
/// <value>
/// The inventory status.
/// </value>
public int InventoryStatus
{
get
{
return lineItem.InventoryStatus;
}

set
{
lineItem.InventoryStatus = value;
}
}

/// <summary>
/// Gets or sets the line item ordering.
/// </summary>
/// <value>
/// The line item ordering.
/// </value>
public DateTime LineItemOrdering
{
get
{
return lineItem.LineItemOrdering;
}

set
{
lineItem.LineItemOrdering = value;
}
}

/// <summary>
/// Gets or sets the configuration id. The external component configuration id, used by bundle, kits and other
/// combination products.
/// </summary>
/// <value>
/// The configuration id.
/// </value>
public string ConfigurationId
{
get
{
return lineItem.ConfigurationId;
}

set
{
lineItem.ConfigurationId = value;
}
}

/// <summary>
/// Gets or sets the provider id. Used to identify the line item in the extrnal system.
/// </summary>
/// <value>
/// The provider id.
/// </value>
public string ProviderId
{
get
{
return lineItem.ProviderId;
}

set
{
lineItem.ProviderId = value;
}
}

/// <summary>
/// Gets or sets the RMA item reason. ("Corrupted", "Mismatch" etc.)
/// </summary>
/// <value>
/// The rma reason.
/// </value>
public string ReturnReason
{
get
{
return lineItem.ReturnReason;
}

set
{
lineItem.ReturnReason = value;
}
}

/// <summary>
/// Gets or sets the returned in stock quantity
/// </summary>
/// <value>
/// The return quantity.
/// </value>
public Decimal ReturnQuantity
{
get
{
return lineItem.ReturnQuantity;
}

set
{
lineItem.ReturnQuantity = value;
}
}

/// <summary>
/// Gets or sets the identity orig line item for RMA line item
/// </summary>
/// <value>
/// The rma orig line item id.
/// </value>
public int? OrigLineItemId
{
get
{
return lineItem.OrigLineItemId;
}

set
{
lineItem.OrigLineItemId = value;
}
}

/// <summary>
/// Gets or sets the warehouse code
/// </summary>
/// <value>
/// The warehouse code.
/// </value>
public string WarehouseCode
{
get
{
return lineItem.WarehouseCode;
}

set
{
lineItem.WarehouseCode = value;
}
}

/// <summary>
/// Gets or sets a value indicating whether if
/// the inventory for this item has already been allocated from
/// the inventory system
/// </summary>
/// <value>
/// The is inventory allocated.
/// </value>
public bool IsInventoryAllocated
{
get
{
return lineItem.IsInventoryAllocated;
}

set
{
lineItem.IsInventoryAllocated = value;
}
}

/// <summary>
/// Gets or sets the old quantity.
/// </summary>
/// <value>
/// The old quantity.
/// </value>
public Decimal OldQuantity
{
get
{
return lineItem.OldQuantity;
}

set
{
lineItem.OldQuantity = value;
}
}

/// <summary>
/// Sets the parent.
/// </summary>
/// <param name="parent">
/// The parent.
/// </param>
public void SetParent(object parent)
{
lineItem.SetParent(parent);
}

/// <summary>
/// Accepts the changes.
/// </summary>
public void AcceptChanges()
{
lineItem.AcceptChanges();
}

/// <summary>
/// Gets the object data.
/// </summary>
/// <param name="info">
/// The info.
/// </param>
/// <param name="context">
/// The context.
/// </param>
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
lineItem.GetObjectData(info, context);
}

/// <summary>
/// Deletes the object data
/// </summary>
public void Delete()
{
lineItem.Delete();
}
}
}

So now we can finally write our test.

CartWrapper = new Mock<ICartHelperWrapper>();

factoryMock.Setup(x => x.GetCartHelper()).Returns(CartWrapper.Object);

var firstLineItem = new Mock<ILineItemWrapper>();

firstLineItem
.SetupGet(x => x.DisplayName)
.Returns("My Test Product");

firstLineItem
.SetupGet(x => x.CatalogEntryId)
.Returns("Borderlands - PC");

firstLineItem
.SetupGet(x => x.Quantity)
.Returns(BorderlandsQnty);

firstLineItem
.SetupSet(x => x.Quantity = It.IsAny<Dec

Show more