How to verify contents of IEnumerable Collection

Asked by Dale King

First off do not reply to just say to use Is.EquivalentTo. That does not work for the case I am asking here.

I have a class that has a property that I want to verify in nUnit. That property is declared as an IEnumerable for some interface type. e.g IEnumerable<ISomeInterface>.

The interface itself has a number of properties defined in the interface.

I want to verify the items that property returns based on those properties. The order of the IEnumerable does not matter.

So if for example if I had:

public interface ISomeInterface
{
    string Property1 { get; }
    string Property2 { get; }
    string Property3 { get; }
}

This would is a simple thing to verify using FitNesse and a RowFixture. I just do:

|use|property|
|Property1|Property2|Property3|
| a | b | c |
| d | e | f |

But I cannot find a clean way to do this with nUnit. The only way I have come up with is:

            Assert.That(property, Has.Count.EqualTo(2));
            Assert.That(property, Has.Some
                .With.Property("Property1").EqualTo("a")
                .With.Property("Property2").EqualTo("b")
                .With.Property("Property3").EqualTo("c"));
            Assert.That(property, Has.Some
                .With.Property("Property1").EqualTo("d")
                .With.Property("Property2").EqualTo("e")
                .With.Property("Property3").EqualTo("f"));

This would not work if I wanted to verify that it has multiple of the same values.

IsEquivalentTo seems to be the thing I should be using, but it would not work. It works for collections of simple value types. It ends up trying to use Object.equals between a constraint and the actual instance of ISomeInterface.

Note that the testing code has no access to any concrete implementations of ISomeInterface so there is nothing to create to check equality with. The concrete implementations do not even have setters for the properties. The properties are all generated from other data, which is part of what I am trying to test.

So what I really need is some equivalent to a FitNesse RowFixture.

Question information

Language:
English Edit question
Status:
Answered
For:
NUnit V2 Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Charlie Poole (charlie.poole) said :
#1

I've linked the bug report in which you posted a revised version of your code using EquvalentTo.

I think the code you've come up with is a big improvement on what's listed in this question. See my comment in response for a simpler version.

We could also discuss improvements to NUnit syntax in order to support tests like yours more cleanly. The best place to do that will be on the nunit-discuss list, where more people can get involved with sharing ideas.

Can you help with this problem?

Provide an answer of your own, or ask Dale King for more information if necessary.

To post a message you must log in.