gpt4 book ai didi

c# - 如何使用 Rhino Mocks 模拟 protected 计算只读属性?

转载 作者:行者123 更新时间:2023-11-30 18:32:33 25 4
gpt4 key购买 nike

我有这样的东西:

public class SomeClass
{
protected ISomeInterface SomeProperty
{
get { return SomeStaticClass.GetSomeInterfaceImpl(); }
}

public void SomeMethod()
{
// uses SomeProperty in calculations
}
}

我如何测试 SomeMethod,使用 Rhino Mocks 模拟 SomeProperty?我正在考虑获取访问器,使用 IL 重写访问器,只是为了返回模拟代理。这听起来有多疯狂?

最佳答案

您不能模拟被测类,只能模拟依赖项。因此,如果您使用某种工厂而不是 SomeStaticClas 并使用 SomeClass 的构造函数参数注入(inject)它,您就可以模拟工厂类。

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();
}

关于c# - 如何使用 Rhino Mocks 模拟 protected 计算只读属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18539810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com