做TDD并想隔离被测方法:Direct();
但是,当测试创建时MyClass,SomeClass.SetupStuff();炸毁(NotImplementedException)。所以,修改了IMyClass接口,使其具有构造Configure();后可以调用的方法,MyClass以避免异常。
问题:这是处理这种情况的一种可接受的方式,还是有一些基本的 OOP 原则会破坏?
public class MyClass : IMyClass
{
public MyClass()
{
// class with static method that sets stuff up
SomeClass.SetupStuff();
}
public void IMyClass.Direct()
{
// want to test this
}
}
对比
public class MyClass : IMyClass
{
public MyClass()
{
}
public void IMyClass.Direct()
{
// want to test this
}
//
public void IMyClass.Configure()
{
// class with static method that sets stuff up
SomeClass.SetupStuff();
}
}