Quote (2012xxxx @ Mar 19 2014 10:34pm)
I first had the expectedTotal to be any wild miscalculated number, so it gives me a failed when I ran the test. Is this what you're referring to (that I was suppose to do)? Sorry, I'm still very new.
The purpose of a unit test is to assert that the unit performs as it was expected.
What does CreateOrder actually do?
Code
public int CreateOrder(Order order)
{
decimal orderTotal = 0;
var cartItems = GetCartItems();
// Iterate over the items in the cart, adding the order details for each
foreach (var item in cartItems)
{
var orderDetail = new OrderDetail
{
ProductID = item.ProductId,
OrderId = order.OrderId,
UnitPrice = item.Product.Price,
Quantity = item.Count
};
// Set the order total of the shopping cart
orderTotal += (item.Count * item.Product.Price);
productDb.OrderDetails.Add(orderDetail);
}
// Set the order's total to the orderTotal count
order.Total = orderTotal;
// Save the order
productDb.SaveChanges();
// Empty the shopping cart
EmptyCart();
// Return the OrderId as the confirmation number
return order.OrderId;
}
Looking over this method...I can see that it is probably very most likely NOT actually creating an order. You aren't doing anything with the Order object you pass in. You are just setting Total on it and then returning the Order.Id. Why pass the Order into the method to begin with? Why not just construct the Order within the method and then return it?
In anycase, assuming it is correct, you want to Assert that the return of CreateOrder is the expected value. Did you modify Order by reference? Assert that the contents of Order is what you expected. Did CreateOrder modify the ShoppingCart? Assert the changes were what you expected (wouldn't do this...not technically a unit test).
Once you have you asserts set up, run the test. Do they pass? Yes? Then your unit is correct. Do they fail? Then your unit has bugs.