Reputation: 2655
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);
Upvotes: 0
Views: 456
Reputation: 8909
Or you use Fluent Assertions to verify your object against an anonymous object that contains the expected properties and values. See http://www.dennisdoomen.net/2012/09/asserting-object-graph-equivalence.html
Upvotes: 0
Reputation: 414
If your rectangle is some sort of a value object, you could rely on the .Equals
method to compare the whole object at a time.
Or, you could just append to a list the errors.
var errors = new List<String>()
if(croppingRectangle.Prop == ExpectedValue) {
//test
}
else {errors.add("ErrorMessage");}
.... and so on
Assert.IsEqual(errors.Count, 0);
Upvotes: 0
Reputation: 1503180
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.
Upvotes: 1