How to test properties atomically in nunit

Given an object with several properties, say System.Drawing.Rectangle, I wanted to assert the values of ALL the properties (not stopping when ONE property didn't match) and report ALL the properties.

I tried this code, hoping it would do what I wanted...

System.Drawing.Rectangle croppingRectangle = SomeMethodReturnsRectangle(testP1,testP2);
Assert.That(()=>{ croppingRectangle.X==testX && croppingRectangle.Y==testY },"expected X={0}, Y={1} but was X={2},Y={3}", testX,testY,croppingRectangle.X,croppingRectangle.Y);

Whats the correct way in NUnit to do this?

(I realize this works:)

if(croppingRectangle.X==testX && croppingRectangle.Y==testY) {
    Assert.Pass();
else
    Assert.Fail("expected X={0}, Y={1} but was X={2},Y={3}", testX,testY,croppingRectangle.X,croppingRectangle.Y);
Jon Skeet
people
quotationmark

I'm assuming you don't want to make the type itself check for equality and override ToString? Because that would do it nice.

One option would be to use anonymous types to accomplish the same goal:

Assert.AreEqual(new { X = testX, Y = testY },
                new { croppingRectangle.X, croppingRectangle.Y });

Due to the way anonymous types work (with Equals and ToString being autogenerated) this should give you a nice error message and check all the properties at the same time. It does rely on the per-property equality check being the default check for each property type though.

people

See more on this question at Stackoverflow