In my C# unit tests, I often query for a list of rows based on a list of IDs. I then want to ensure that 1) for all the IDs, there was at least one row found that has that ID and 2) for all the returned rows, each row has an ID that is in the list of IDs to find. Here is how I usually ensure that:
Assert.IsTrue(ids.All(
id => results.Any(result => result[primaryKey].Equals(id))
), "Not all IDs were found in returned results");
Assert.IsTrue(results.All(
result => ids.Any(id => result[primaryKey].Equals(id))
), "Returned results had unexpected IDs");
I think the use of Any
and All
is convenient for such checks, but I wanted to see if anyone thinks this is less readable than it could be, or if there is perhaps a nicer way of doing two-way checks like this. I'm using MSTest in Visual Studio 2008 Team System for unit testing. This perhaps should be community wiki if it's too subjective.
Edit: I'm now using a solution based on Aviad P.'s suggestion, and also the fact that the following test passes:
string[] ids1 = { "a", "b", "c" };
string[] ids2 = { "b", "c", "d", "e" };
string[] ids3 = { "c", "a", "b" };
Assert.AreEqual(
1,
ids1.Except(ids2).Count()
);
Assert.AreEqual(
2,
ids2.Except(ids1).Count()
);
Assert.AreEqual(
0,
ids1.Except(ids3).Count()
);