Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why or how to use NUnit methods with ICollection<T>

Some of NUnit's Assert methods are overloaded to use ICollection but not ICollection<T> and thus you can't use them.

Is there anyway around this? Heck, am I doing something stupid?

I'm having to drop back to using Assert.AreEqual rather than specialised methods and its making my tests ugly.

Any advice?

Edit:

Thanks for the responses. The That method of NUnit seems interesting so I'll look into it at a later date.

Mark correctly mentioned this, but NUnit Collection Asserts are excellent. I've recently used them on some new tests and found them excellent to work with.

like image 852
Finglas Avatar asked Nov 24 '09 14:11

Finglas


3 Answers

I don't know if this is what you're looking for, but for generic collections instead of using:

Assert.Contains(member, list);

I use:

Assert.That(list.Contains(member));

which I find almost as readable.

like image 95
Rian Schmits Avatar answered Nov 15 '22 18:11

Rian Schmits


ICollection and ICollection<T> are different contracts - one does not inherit the other.

http://msdn.microsoft.com/en-us/library/system.collections.icollection_members.aspx http://msdn.microsoft.com/en-us/library/y2fx0ty0.aspx

If you have a generic collection you can call ToList() on it and get a List<T>, which happens to implement the non-generic ICollection as well. Then use that List in the NUnit Assert method.

like image 26
Amy B Avatar answered Nov 15 '22 20:11

Amy B


There are a set of CollectionAsserts, or you could inherit your test from AssertHelper and use syntax like

Expect(actual, Is.EquivalentTo(expected));

A look at the documentation should give you the syntax for the constraints that apply to collections.

Here's a link (this is version 2.5.2)

N.B. Expect is just shorthand for Assert.That...

like image 30
Mark Dickinson Avatar answered Nov 15 '22 19:11

Mark Dickinson