dimanche 18 février 2018

NUnit 3 CollectionConstraint custom IComparer not working

Question

How can I correctly use a custom IComparer with NUnit 3 collection constraints for a collection containing a number of custom interface references

What I have tried

I have a class that returns an instance of IEnumerable<IInterface> where IInterface is defined as follows:

public interface IInterface {

    string PropertyOne { get; }

    string PropertyTwo { get; }

}

I am trying to use collection constraints from NUnit 3 with a custom IComparer, but it is not working. To create a mock object to test against, I'm using Moq. The test looks roughly like this:

[Test]
public void MyTest() {

    var mockObject = new Mock<IInterface>();
    mockObject.Setup(x => x.PropertyOne).Returns("Foo");
    mockObject.Setup(x => x.PropertyTwo).Returns("Bar");

    var collection = myObject.Collection;

    Assert.That(collection, Contains.Item(mockObject).Using(this);
}

The test case is contained in a class that inherits from the following base class

public class TestBase : IComparer<IAssembly> {
    public int Compare(IInterface one, IInterface two) { /* some code */ }
}

When I set a breakpoint on the above Compare method, I'm finding that NUnit is completely ignoring my comparer, chosing to use the ToString() implementation on the underlying object, despite the fact that it isn't exposed by the interface.

Aucun commentaire:

Enregistrer un commentaire