我在 VS2012 中使用 Microsoft Fakes 框架。
我使用下面的代码来填充instance我的类型的方法。
using (ShimsContext.Create())
{
ShimDateTime.NowGet = () => { return new DateTime(1949, 10, 1); };
DateTime now = DateTime.Now; // shim works for the static property DateTime.Now.
Class1 dependency = new Class1();
using (ShimsContext.Create())
{
ShimClass1 shim1 = new ShimClass1();
StubClass1 stub1 = new StubClass1();
shim1.method1 = () => { return "shim method1"; };
shim1.IMethod1 = () => { return "shim IMethod1"; };
String s1 = dependency.method1();// shim doesn't work for the instance method.
String s2 = dependency.IMethod1();// shim doesn't work for the instance method.
}
class1 看起来像这样:
public class Class1 : Interface1
{
public String method1()
{
return "real method1";
}
//Interface 1 member
public string IMethod1()
{
return "real IMethod1";
}
}
我希望 s1 和 s2 是匀场输出,但它仍然是真实输出。
Why?
如果“method1”是静态的,那么您的垫片就会起作用。然而,使用当前的代码,您还没有真正消除“method1”。您需要将该实例与 shim 实例关联
Class1 依赖 = new ShimClass1() { Method1 = () => { return "Shim.Method1"; } };
或将所有实例方法与您的委托相关联
ShimClass1.AllInstances.Method1 = (q)=> { return "Shim.Method1"; };
另外,我认为不需要执行两次 ShimsContext.Create()
如果您想使用存根来重定向 IMethod1,您应该使用 StubInterface1
Class1 依赖 = new StubInterface1() { Method1 = () { return ""; } };
这些的变体可以在 msdn 上找到以供参考
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)