【发布时间】:2012-01-26 08:02:38
【问题描述】:
public class MyBaseClass
{
protected void OnSomeEvent(EventContext context)
{
SomeType = _someService.DoSomeLogic(context);
}
public SomeType SomeType { get; private set; }
}
MyBaseClass 在流程管道中触发事件时会发挥一些魔力来初始化公共属性值。
public class MyClass : MyBaseClass
{
public string Foo()
{
if (SomeType.SomeLogic())
throw new InvalidOpertaionException();
return "huzzah";
}
}
其他类中的流程依赖于SomeType被初始化。
如何对MyClass.Foo() 进行单元测试?
[TestMethod]
public void Foo_ReturnsHuzzah()
{
var result = new MyClass().Foo();
Assert.IsHuzzah(result);
}
【问题讨论】:
-
我认为 SomeType 不应该有那个公共设置器。
-
@Sign 啊是的好地方。 setter 应该是私有的。
标签: c# unit-testing tdd protected base-class