How to use CollectionAssert (and Linq?) to find all the values for one property, are the same?
-
25-09-2019 - |
Question
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?
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow