I spend over 2 hours on this... i can't figure out why this test is PASSING. I mean.. it shouldnt return the view "Completed" but in the test it does! It say Expected "string.Empty" but returned "Completed" however for the creditcard to pass.. the securitycode need to be "test" which is isnt at all in the test. So it should return the default view (which is like view();).
What i am doing wrong ? it is my test that is wrong ? or the controller logic ?
Thanks a lots.
[Test]
public void Cannot_Check_Out_If_Credit_Card_Failed_To_Process()
{
var mockOrderSubmitter = new Mock<IOrderSubmitter>();
var mockCreditCardProcessor = new Mock<ICreditCardProcessor>();
// Arrange: Given a user has a non-empty cart
var cart = new Cart();
cart.AddItem(new Product(), 1);
// Arrange: ... but the credit card failed to process
var cartController = new CartController(null, mockOrderSubmitter.Object, mockCreditCardProcessor.Object);
var result = cartController.CheckOut(cart, new ShippingDetails(), new CreditCard() { SecurityCode = "123" });
// Assert
result.ShouldBeDefaultView();
}
[HttpPost]
public ActionResult CheckOut(Cart cart, ShippingDetails shippingDetails, CreditCard creditCard)
{
// Empty carts can't be checked out
if (cart.Lines.Count == 0)
ModelState.AddModelError("Cart", "Sorry, your cart is empty!");
// Everything is valid
if (ModelState.IsValid)
{
// Effectue le paiement.
TransactionResul开发者_如何学Pythont result = creditcardProcessor.TakePayment(creditCard, cart.ComputeTotalValue());
if (result == TransactionResult.Success)
{
// Envoi la commande
orderSubmitter.SubmitOrder(cart, shippingDetails);
cart.Clear();
return View("Completed");
}
else
{
ModelState.AddModelError("CreditCard", "Sorry, we couldn't process your credit card, please verify your credit card details and retry.");
return View(new CheckOutViewModel());
}
}
else // Something was invalid
return View(new CheckOutViewModel());
}
public class MainCreditCardProcessor : ICreditCardProcessor
{
public TransactionResult TakePayment(CreditCard card, decimal amount)
{
if (card.SecurityCode == "test")
return TransactionResult.Success;
else
return TransactionResult.TransactionDeclined;
}
}
Found solution to this. The mocks object doesnt have any implementation. (Which i should have know before that)
精彩评论