How-to assert that a collection has exactly one element satisfying a condition?

1,307 views
Skip to first unread message

haifisch

unread,
Oct 5, 2011, 2:23:49 AM10/5/11
to NUnit-Discuss
Hello,

I search for something similar to the pseudo-code
"Assert.That(myCollection, Has.One.Matches(myCondition))".
How can i state such an assert with NUnit? Is it possible?

Thanks for any help.

Best regards,

Andreas

Oliver Gramberg

unread,
Oct 5, 2011, 5:09:54 AM10/5/11
to NUnit-Discuss
Hi Andreas,

> "Assert.That(myCollection, Has.One.Matches(myCondition))".
> How can i state such an assert with NUnit? Is it possible?

maybe not natively using NUnit syntax, but if LINQ is an option, try
something like this:

IEnumerable<int> enumerable = myCollection.OfType<int>();
int c = enumerable.Count(e => e >= 5);
Assert.That(c, Is.EqualTo(1));

or, in one go,

Assert.That(myCollection.OfType<int>().Count(e => e >= 5),
Is.EqualTo(1));


Best regards,
Oliver

haifisch

unread,
Oct 5, 2011, 6:08:59 AM10/5/11
to NUnit-Discuss
Hi Oliver,

I couldn't believe that I never have thought that :-(

Many thanks for your quick and smart answer!

Andreas

Michael Powell

unread,
Oct 5, 2011, 6:15:03 AM10/5/11
to nunit-...@googlegroups.com
On Wed, Oct 5, 2011 at 4:08 AM, haifisch <andreas...@googlemail.com> wrote:
Hi Oliver,

I couldn't believe that I never have thought that :-(

Many thanks for your quick and smart answer!

That's a "thinking outside the box" type answer. I find NUnit has a few tools in my toolbox, and I am frequently jumping outside the box to elaborate a richer test environment along these lines. :)

Andreas

On 5 Okt., 11:09, Oliver Gramberg <oliver.gramb...@gmx.de> wrote:
> Hi Andreas,
>
> > "Assert.That(myCollection, Has.One.Matches(myCondition))".
> > How can i state such an assert with NUnit? Is it possible?
>
> maybe not natively using NUnit syntax, but if LINQ is an option, try
> something like this:
>
> IEnumerable<int> enumerable = myCollection.OfType<int>();
> int c = enumerable.Count(e => e >= 5);
> Assert.That(c, Is.EqualTo(1));
>
> or, in one go,
>
> Assert.That(myCollection.OfType<int>().Count(e => e >= 5),
> Is.EqualTo(1));
>
> Best regards,
> Oliver

--
You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
To post to this group, send email to nunit-...@googlegroups.com.
To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nunit-discuss?hl=en.


Charlie Poole

unread,
Oct 5, 2011, 10:25:38 AM10/5/11
to nunit-...@googlegroups.com

Quite right.

That said, should we support One or Exactly(n)?

Charlie

Oliver Gramberg

unread,
Oct 7, 2011, 5:05:28 AM10/7/11
to NUnit-Discuss
> That said, should we support One or Exactly(n)?

System.Linq.Enumerable has extension methods All() and Any(), which
match NUnit's Has.All and Has.Some. NUnit also provides Has.None,
Enumerable also provides Count() with a predicate as parameter.

So, yes, I think Has.Exactly(n) could be useful (I believe I needed it
once and had to use LINQ like in my earlier post.) Has.None is
Has.Exactly(0), Has.All is Has.Exactly(x.Count()) - checking for
uniqueness of one certain value is probably also an important case, so
it makes sense to have Has.One (which is Has.Exactly(1)).

What about Has.AtLeast(n) and Has.AtMost(n)? What about Has.Max(value)
and Has.Min(value)? Well, this could go on forever... Charlie, has a
decision been made yet on extensibility, such that these could be
extension methods provided by an add-on or the tester herself?


Oliver

Michael Powell

unread,
Oct 7, 2011, 6:15:43 AM10/7/11
to nunit-...@googlegroups.com
At least from my perspective...


This type of thing seems useful to me. It's the kind of thing, if my tests call for something like that, I'll use the extensions if they are there, or I will roll my own as needs be.

Kenneth Xu

unread,
Oct 7, 2011, 6:21:04 PM10/7/11
to nunit-...@googlegroups.com
This remind me another post about similar requirement. https://groups.google.com/forum/#!msg/nunit-discuss/EQE7Nz7vIzQ/tC3F8iMAYYQJ

I find myself jumping out of the box more and more using expression for assertion either with a lib like ExpressionToCode if I'm allowed, or roll my own (shamelessly link to my blog for detail: http://kennethxu.blogspot.com/2011/08/use-linq-expression-for-unit-test.html).

Cheers,
Kenneth

Charlie Poole

unread,
Oct 19, 2011, 1:52:51 PM10/19/11
to nunit-...@googlegroups.com

Charlie Poole

unread,
Oct 20, 2011, 2:19:40 PM10/20/11
to nunit-...@googlegroups.com
The 2.6 code base now has Exactly(n). I decided not to support 'One'
since it's easy
and clearer to write 'Exactly(1)'.

Charlie

Reply all
Reply to author
Forward
0 new messages