NSubstitute 在其文档中这样说:
返回接口的方法 [...] 将自动返回自己的替代品。
通常这就足够了。但是,当我这样做时:
测试方法:
IUnityContainer unity = Substitute.For<IUnityContainer>();
MyMethod(unity);
实际方法:
public void MyMethod(IUnityContainer container)
{
this.container = container;
myObject = container.Resolve<ISomeObject>();
myObject.CallSomeMethod();
}
Resolve 方法返回一个类。所以它没有被嘲笑。这意味着当我调用时我在 myObject 中得到 null 和 null 引用异常CallSomeMethod
;
如果我可以只返回一个模拟类(除非我专门覆盖了该接口),那就太好了。
有没有办法使用NSubstitute来获得这个?