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.

10
  • Does OnlineSignupModel derive from IEnumerable?
    – Nkosi
    Commented Apr 26, 2018 at 15:50
  • This may be an XY problem. What are you actually trying to do?
    – Nkosi
    Commented Apr 26, 2018 at 15:51
  • I am trying to assert that there are 5 properties in the object. Commented Apr 26, 2018 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.
    Commented Apr 26, 2018 at 15:56
  • 1
    I understand the silliness of the task but it was assigned to me by my boss. Commented Apr 26, 2018 at 15:58

2 Answers 2

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.