为什么这个测试单元通过了?
我花了两个多小时在这上面......我不明白为什么这个测试通过了。我的意思是..它不应该返回“已完成”视图,但在测试中它确实返回了!它说预期“string.Empty”但返回“Completed”但是信用卡要通过..安全代码需要是“测试”,而测试中根本没有。所以它应该返回默认视图(类似于 view();)。
我做错了什么?是我的测试错了吗?或者控制器逻辑?
非常感谢。
[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.
TransactionResult 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;
}
}
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.
TransactionResult 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了解决这个问题的方法。模拟对象没有任何实现。 (我应该在那之前就知道)
Found solution to this. The mocks object doesnt have any implementation. (Which i should have know before that)