D.R. D.R. - 1 year ago 80
C# Question

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


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
constraint and no equivalent
- what am I missing?

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

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

Answer Source

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"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download