Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What makes a class a collection according to NUnit collection constraints?

I have a class that implements ICollection<SomeConcreteClass>. The NUnit collections constraints do not recognize it as a collection.

e.g. Assert.That( sut, Has.No.Member( someObjectOfTypeSomeConcreteClass ) ); throws System.ArgumentException : The actual value must be a collection

and Assert.That( sut, Is.Empty ); fails with empty sut.

So when is a collection a collection (according to NUnit)?

Stack Trace:

System.ArgumentException : The actual value must be a collection Parametername: actual
at NUnit.Framework.Constraints.CollectionConstraint.Matches(Object actual)
at NUnit.Framework.Constraints.NotConstraint.Matches(Object actual)
    MyTestFile.cs(36,0): at MyAssembly.MyTestFixture.MyTestMethod()

Above problems occurred with NUnit 2.4.3.0. I just tried it with 2.6. Is.Empty works now, but Has.No.Member still fails. It does not even call Equals() or operator ==(). How does it compare the collection elements? RhinoMocks Arg<MyCollection>.List.Count( Is.Equal( 1 ) ) does now fail too.

Conclusion:
With NUnit 2.4 the collection constraints require implementation of non-generic ICollection for the collection to be recognized as a collection (that answers the original question). IEnumerable equality works as expected.

With NUnit 2.6 (and possibly 3.0) equality of IEnumerables is checked by matching elements even if Equals is overridden. That's why the membership constraint does not work if the elements are IEnumerables themselves. This is a known issue (https://bugs.launchpad.net/nunit-3.0/+bug/646786).

For details see my own answer.

like image 712
EricSchaefer Avatar asked Oct 09 '22 10:10

EricSchaefer


1 Answers

After looking into the source code of NUnit 2.5.10: The constraint first casts the given collection to the non-generic IEnumerable.

EDIT: Then it runs a foreach() over the collection and compares the items. So AFAICT it should work.

What version of NUnit are you using?

like image 176
Daniel Rose Avatar answered Oct 12 '22 11:10

Daniel Rose