2

Why the below assertion is not working?

Code:

        [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);
        }

Error:

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)

I am trying to assert that there are 5 properties in the object.

Dean Friedland
  • 773
  • 2
  • 12
  • 32
  • Does `OnlineSignupModel` derive from `IEnumerable`? – Nkosi Apr 26 '18 at 15:50
  • This may be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you actually trying to do? – Nkosi Apr 26 '18 at 15:51
  • I am trying to assert that there are 5 properties in the object. – Dean Friedland Apr 26 '18 at 15:52
  • 2
    That's not what `Has.Exactly` is for. Why would you even be testing for the existence of 5 properties? It's a class: you either defined the properties or you didn't. It seems like you are trying to test whether or not the properties were assigned to. That's still not a job for `Has.Exactly`. – Kenneth K. Apr 26 '18 at 15:56
  • 1
    I understand the silliness of the task but it was assigned to me by my boss. – Dean Friedland Apr 26 '18 at 15:58
  • time to find another company. your boss clearly doesn't know what he is doing. The number of property is hardcoded in your assembly. There is no way its going to change during run time. – Steve Apr 26 '18 at 16:04
  • Understood. Do you know how to assert that there are n properties in an object? – Dean Friedland Apr 26 '18 at 16:09
  • Use reflection to retrieve how many properties there are at runtime and then assert against that number. https://stackoverflow.com/questions/8151888/c-sharp-iterate-through-class-properties – ToastyMallows Apr 26 '18 at 16:18
  • Thanks bud! I appreciate the help. – Dean Friedland Apr 26 '18 at 16:37
  • @steve, maybe you're right (we've all had such in the past) but there are legitimate reasons to want to do this. Perhaps she is building some code analysis KPI report. Perhaps it is more about understanding whether Dean understands reflection. Either way, the problem is the wrong expectation on the purpose of Has.Exactly. – Adam G Apr 26 '18 at 22:58

2 Answers2

2

Avoiding any commentary on the usefulness of this task, to assert that your model has exactly 5 properties, you can use something like Assert.That(typeof(model).GetProperties().Length == 5);

Chris
  • 79
  • 1
  • 9
2

You are asserting incorrectly with the wrong constraint.

There are multiple ways to assert the model, but here is one.

[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);
}

Consider reviewing the docs on how to use the framework

NUnit Documentation Wiki

Nkosi
  • 235,767
  • 35
  • 427
  • 472