You can do this by installing Mock in a specific class and using As() to retrieve the base IInterface on which to configure. Then you can use mock.Object to call the base specific object:
[Test] public void SomeTest() { var mock = new Mock<DefaultImplementation>().As<IInterface>(); mock.Setup(i => i.Method(It.IsAny<int>())) .Returns<int>(p => p == 0 ? 5 : ((DefaultImplementation)mock.Object).Method(p)); TheMethodITest(mock.Object); }
Here are the rest of the settings I tested with, FWIW:
public interface IInterface { int Method(int p); } public class DefaultImplementation : IInterface { public int Method(int p) { return 6; } } [TestFixture] public class SomeFixture { public static void TheMethodITest(IInterface dep) { for (var i = 0; i < 10; i++) { Debug.WriteLine("{0}:{1}",i, dep.Method(i)); } } }
source share