mercredi 29 juin 2016

Moq with delegate in Returns always returns null

I am trying to make a Mock object for a class, so that the Mock object has access to all of the same methods as the original class, except for the one method I want to perform differently. However, my Mock object has no methods when I view it in Debug mode, and all of the calls I don't want to alter (for testing purposes) return null. Here is the way I am presently doing this:

var originalClass = ContainingClass.OtherContainingClass.ClassIWantToMock;
var originalClassMock = new Mock<IClassToMock> {CallBase = true};

originalClassMock.Setup(c => c.MethodToMock(It.IsAny<paramType>()))
    .Returns<TReturn>(originalClass.MethodToMock);

I get the same result whether I only mock the method I want to change, as well as when I mock every method in the class. I suspect this is because IClassToMock is an interface, even though I am feeding in methods from an object that implements that interface. This is probably why CallBase does not make a difference--I get the same result regardless of whether CallBase is set. Is there a way to mock an interface by setting up methods that run delegates from a class implementing that interface?

Thanks!

Aucun commentaire:

Enregistrer un commentaire