为什么该测试单元通过? [英] Why is this test unit pass?

查看:97
本文介绍了为什么该测试单元通过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此上花了2多个小时...我不知道为什么这个测试通过了.我的意思是..它不应该返回视图"Completed",但是在测试中确实如此!它说期望"string.Empty",但是返回"Completed",以便信用卡通过.安全代码需要是"test",在测试中根本没有.因此,它应该返回默认视图(类似于view();).

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 ?

非常感谢.

[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 found a solution to my problem. To help others poeple that might have the same problem i will explain it.

问题出在我的考试上.由于模拟对象是内部没有方法的空对象,因此逻辑为零.这意味着测试实际上没有使用MainCreditCarDProcessor的实例. (我对此一无所知)

The problem was with my test. Since mocks objects are empty objects with no methods inside, zero logics. That mean that the test actually doesn't use an instance of the MainCreditCarDProcessor. (I didn't know about that)

因此,我必须通过添加以下内容来为此特定测试设置模拟程序:

So i had to setup the mock for this particular test by adding :

// Arrange: Given we have a creditcard processor that return TransactionDeclined.
mockCreditCardProcessor.SetReturnsDefault(TransactionResult.TransactionDeclined);

要强制它返回TransactionDeclined,通过它不是强制",因为它是一个模拟,您只需将其设置为返回该值,否则我想它将返回它找到的默认值.在这种情况下为0枚举的第一个元素,对于返回其他类型值的其他类型方法,则为null.

To force it to return TransactionDeclined,, through this is not "forcing" since its a mock you just set it up to return that value otherwise i guess it will return the default value it find.. which in this case was 0 the first element of the enum or null for other kind of method that return others kind of values.

如果我说的任何话是错误的,请纠正我.

If anything i said is wrong please correct me.

谢谢.

这篇关于为什么该测试单元通过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆