How to use Microsoft Fakes framework to shim an instance method?

两盒软妹~` 提交于 2019-12-04 12:57:17

If 'method1' was static your shim would have worked. However with the current code you have not really shimmed out 'method1'. You need to either associate the instance with the shim instance

Class1 dependency = new ShimClass1() { Method1 = () => { return "Shim.Method1"; } };

or associate all instance methods with your delegate

ShimClass1.AllInstances.Method1 = (q)=> { return "Shim.Method1"; };

Also I dont see the need to have the ShimsContext.Create() done twice

If you want to use stubs to redirect IMethod1 you should be consuming the StubInterface1 instead

Class1 dependency = new StubInterface1() { Method1 = () { return ""; } };

Variations of these are available on msdn for reference

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