为什么这个测试单元通过了?

发布于 2024-10-12 13:01:14 字数 2151 浏览 4 评论 0原文

我花了两个多小时在这上面......我不明白为什么这个测试通过了。我的意思是..它不应该返回“已完成”视图,但在测试中它确实返回了!它说预期“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫烠⑼条掵仅有一顆心 2024-10-19 13:01:14

找到了解决这个问题的方法。模拟对象没有任何实现。 (我应该在那之前就知道)

Found solution to this. The mocks object doesnt have any implementation. (Which i should have know before that)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文