如何使用Rhino Mocks模拟受保护的计算只读属性
本文关键字:计算 只读属性 受保护 模拟 何使用 Rhino Mocks | 更新日期: 2023-09-27 18:29:45
我有这样的东西:
public class SomeClass
{
protected ISomeInterface SomeProperty
{
get { return SomeStaticClass.GetSomeInterfaceImpl(); }
}
public void SomeMethod()
{
// uses SomeProperty in calculations
}
}
如何测试SomeMethod,使用Rhino Mocks模拟SomeProperty?我正在考虑获取访问器,使用IL重写访问器,只是为了返回模拟代理。听起来有多疯狂?
您不能模拟测试中的类,只能模拟依赖项。因此,如果您使用某种工厂而不是SomeStaticLas,并使用SomeClass的构造函数参数注入它,那么您就可以模拟工厂类。
public class SomeClass
{
public SomeClass(ISomeInterfaceFactory factory)
{
this.factory = factory;
}
protected ISomeInterface SomeProperty
{
get { return factory.GetSomeInterface(); }
}
public void SomeMethod()
{
// uses SomeProperty in calculations
}
}
public interface ISomeInterfaceFactory
{
ISomeInterface GetSomeInterface();
}