gpt4 book ai didi

c# - It.IsAny()、IsAny()、Times.Never()。他们在做什么?

转载 作者:行者123 更新时间:2023-11-30 14:01:49 28 4
gpt4 key购买 nike

我想知道你是否可以帮助我理解以下行:

        mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), 
It.IsAny<ShippingDetails>()),
Times.Never());

测试方法:

[TestMethod]
public void Cannot_Checkout_Empty_Cart()
{
// Arrange - create a mock order processor
Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();

// Arrange - create an empty cart
Cart cart = new Cart();

// Arrange - create shipping details
ShippingDetails shippingDetails = new ShippingDetails();

// Arrange - create an instance of the controller
CartController target = new CartController(null, mock.Object);

// Act
ViewResult result = target.Checkout(cart, shippingDetails);

// Assert - check that the order hasn't been passed on to the processor
mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Never());

// Assert - check that the method is returning the default view
Assert.AreEqual("", result.ViewName);

// Assert - check that we are passing an invalid model to the view
Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
}

最佳答案

It.IsAny<Cart>匹配 Cart 类型的任何值。因为你有 Times.Never ,您实际上验证了该方法从未被调用并带有两个参数(Card、ShippingDetails)。

关于c# - It.IsAny<Cart>()、IsAny<ShippingDetails>()、Times.Never()。他们在做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7503854/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com