MOQ - how to mock an interface that needs to be cast to another interface?

此生再无相见时 提交于 2019-11-29 20:23:13
Håvard S

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!