MOQ - how to mock an interface that needs to be cast to another interface?
Solution 1:
The way I understand you, you want to create a mock that implements two interfaces. With Moq, that is as simple as this:
var mock = new Mock<IFoo>(); // Creates a mock from IFoo
mock.As<IBar>(); // Adds IBar to the mock
mock.As<IBar>().Setup(m => m.BarMethod()).Returns(new object()); // For setups.
Now, you can set up expectations and use your mock as you would normally use the object implementing both IFoo
and IBar
.
For your GetNewInstance
method, you can just set up an expectation that returns the mock itself.