Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert "at least one item in the result collection matches predicate"

I want to assert that at least one item of a collection matches a given predicate with NUnit. I already asserted that the number of items is greater than 0, so it suffices to mimic the behavior of LINQ's Any() method.

I'm looking for something alike:

Assert.That(resultEnumerable, Is.Any.Matching(x => x.Property == "x"));

Or at least for:

Assert.That(resultEnumerable.Select(x => x.Property), Is.Any.EqualTo("x"));

Unfortunately, there seems to be only a Is.All constraint and no equivalent Is.Any - what am I missing?

Note: I don't want the much less readable:

Assert.That(resultEnumerable.Any(x => x.Property == "x"), Is.True);
like image 403
D.R. Avatar asked Aug 09 '16 14:08

D.R.


2 Answers

How about one of these?

Assert.That (resultEnumerable, Has.Some.Property ("Property").EqualTo ("x"));
Assert.That (resultEnumerable, Has.Some.Matches<X> (x => x.Property == "x"));
like image 96
Fabian Schmied Avatar answered Oct 02 '22 15:10

Fabian Schmied


I found:

Assert.That (resultEnumerable.Select (x => x.Property), Has.Some.EqualTo ("x"));

Would still prefer a solution where I do not need the Select() anymore.

like image 37
D.R. Avatar answered Oct 02 '22 16:10

D.R.