views:

68

answers:

1

Hi folks,

this is an extension to a previous question I asked, today .... which highlighted the use of CollectionAssert to help test collections (which I never knew).

I have an ICollection<Foo> foos; This has a property called Status, which .. to keep things simple, is an int or byte (whatever floats your boat <-- see what I did there?! /me hides).

So, how can I use CollectionAssert to see if all the items, in the returned ICollection, all have the same value for that one property?

A: 

I don't see anything in CollectionAssert to help, but you could do something like:

int expectedValue = foos.First().Status;
Assert.IsTrue(foos.All(x => x.Status == expectedValue));

It's not ideal in that it won't show you the actual values in case of failure - but you could write your own method for that, if you need to do this regularly.

Jon Skeet
Thanks Jon (again :P). this is great. I constantly forget about the ~All(..)~ extension method. This will work like a treat :)
Pure.Krome