Moq setup does not match parameter because it is a subtype?

397 views Asked by At

I'm trying to use Setup on a Moq like I've done many times to return a value, but I can't seem to understand why the parameter matching fails in this case.

Here's what I'm doing:

var myParameter = new SomeParameter();
myMock.Setup(x => x.SomeFunction(myParameter)).Returns(myResult);

Where SomeParameter : IParameter and the function is defined as SomeFunction(IParameter parameter).

If I change the first line to:

IParameter myParameter = new SomeParameter();

or indeed, cast it:

myMock.Setup(x => x.SomeFunction((IParameter)myParameter))).Returns(myResult);

It works fine.

Is this normal behavior, or a bug? Can I configure Moq to not check the compile-time type of whatever parameter I'm passing in?

Using Moq 4.5.30, slightly older version.

0

There are 0 answers