私の Web ストア アプリには、アイテムの合計値を追加、削除、および計算できる「カート」クラスがあります。データ モデルは次のとおりです。1 つのアイテムには 1 つの製品と 1 つの配送が含まれます。Product には Price フィールドがあり、Shipping には Cost フィールドがあります。Cart クラスのコードは次のとおりです。
public class CartLine
{
public Item Item { get; set; }
public int Quantity { get; set; }
}
public class Cart
{
private List<CartLine> lineCollection = new List<CartLine>();
// methods:
// Add(Item item, int quantity)
// Remove(Item item)
public decimal ComputeTotalProductValue()
{
return lineCollection.Sum(l => l.Item.Product.Price*l.Quantity);
}
// methods with the same principle:
// ComputeTotalShippingValue()
// ComputeOveralValue()
}
そして、これが私の単体テストです(もちろん動作しません):
[TestMethod]
public void Can_Compute_Total_Values()
{
// Arrange
var itemMock = new Mock<IItemsRepository>();
itemMock.Setup(i => i.GetItems()).Returns(new[]
{
new Item { ItemId = 1, ProductId = 1, ShippingId = 1 },
new Item { ItemId = 2, ProductId = 2, ShippingId = 2 }
});
var productMock = new Mock<IProductRepository>();
productMock.Setup(p => p.GetProducts()).Returns(new[]
{
new Product { ProductId = 1, Price = 10 },
new Product { ProductId = 2, Price = 20 }
});
var shippingMock = new Mock<IShippingRepository>();
shippingMock.Setup(s => s.GetShippings()).Returns(new[]
{
new Shipping { ShippingId = 1, Cost = 2 },
new Shipping { ShippingId = 2, Cost = 5 }
});
var item1 = itemMock.Object.GetItems().ToArray()[0];
var item2 = itemMock.Object.GetItems().ToArray()[1];
var target = new Cart();
//Act
target.Add(item1, 2);
target.Add(item2, 4);
decimal totalProduct = target.ComputeTotalProductValue();
decimal totalShipping = target.ComputeTotalShippingValue();
decimal overalSum = target.ComputeOveralValue();
// Assert
Assert.AreEqual(totalProduct, 100);
Assert.AreEqual(totalShipping, 24);
Assert.AreEqual(overalSum, 124);
}
}
この問題は、おそらくアイテムを製品にバインドし、配送することに関連しています。これどうやってするの?
前もって感謝します!