Mocking method with various signatures in which the object has a parameter type

I have the following interfaces

public interface IInfo
{
    bool IsCompatibleWith (Object informationObject);
}

public interface IInfo<T> : IInfo
{
    bool IsCompatibleWith (T informationObject);
}

      

and try the following Mocks

Foo f = new Foo();
Mock<IInfo<Foo>> infoMock = new Mock<IInfo<Foo>>();
infoMock.Setup(i => i.IsCompatibleWith(f)).Returns(true);

      

Then the following lines are checked

IInfo mockedInfo;
mockedInfo.IsCompatibleWith(f);

      

The problem is that the install method installs IsCompatibleWith (T informationObject)

and the code calls IsCompatibleWith (Object informationObject)

. How do I set up both signatures?

+3


source to share


1 answer


The following snippet shows a way to configure both methods:

//configure the method with the `object` as a parameter
infoMock.Setup(i => i.IsCompatibleWith((object)f)).Returns(true);

//configure the method with the `IModel` as a parameter
infoMock.Setup(i => i.IsCompatibleWith(f)).Returns(true);

      



Moq

writes the arguments as they are. When you point your instance to object

, the method bool IsCompatibleWith(Object informationObject)

will accept registration

+7


source







All Articles