首页 > 解决方案 > N-Unit Has.Exactly(n).Items 断言抛出错误

问题描述

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

代码:

        [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 个属性。

标签: c#unit-testingnunit

解决方案


避免对此任务的有用性进行任何评论,断言您的模型恰好具有 5 个属性,您可以使用类似Assert.That(typeof(model).GetProperties().Length == 5);


推荐阅读