gpt4 book ai didi

c# - N-Unit Has.Exactly(n).Items 断言抛出错误

转载 作者:行者123 更新时间:2023-11-30 23:03:08 26 4
gpt4 key购买 nike

为什么下面的断言不起作用?

代码:

        [Test]
public void CreateNewTemplateTest()
{
OnlineSignupModel model = new OnlineSignupModel
{
SalesRepId = 68,
PriceAdvanced = (decimal)22.33,
PriceComplete = (decimal)44.33,
PriceMvr = (decimal)6.33,
SetupFee = (decimal)2.33,
};

Assert.That(model, Has.Exactly(5).Items);
}

错误:

System.ArgumentException : The actual value must be an IEnumerable
Parameter name: actual
at NUnit.Framework.Constraints.ExactCountConstraint.ApplyTo[TActual](TActual actual)
at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression, String message, Object[] args)
at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression)

我试图断言对象中有 5 个属性。

最佳答案

您使用错误的约束进行了错误的断言。

断言模型的方法有多种,这里只介绍一种。

[Test]
public void CreateNewTemplateTest() {
//Arrange
var salesRepId = 68,
var priceAdvanced = (decimal)22.33,
var priceComplete = (decimal)44.33,
var priceMvr = (decimal)6.33,
var setupFee = (decimal)2.33,

//Act
OnlineSignupModel model = new OnlineSignupModel {
SalesRepId = salesRepId,
PriceAdvanced = priceAdvanced,
PriceComplete = priceComplete,
PriceMvr = priceMvr,
SetupFee = setupFee,
};

//Assert
Assert.That(
model.SalesRepId = salesRepId &&
model.PriceAdvanced == priceAdvanced &&
model.PriceComplete == priceComplete &&
model.PriceMvr == priceMvr &&
model.SetupFee == setupFee, Is.True);
}

考虑查看有关如何使用该框架的文档

NUnit Documentation Wiki

关于c# - N-Unit Has.Exactly(n).Items 断言抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50047084/

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