【发布时间】:2012-06-17 17:16:22
【问题描述】:
我正在尝试接近 100% 的代码覆盖率,并且我对模拟 OpenFileDialog 很感兴趣。从一些研究来看,一个好的答案似乎是创建一个 IFileDialogService,就像来自 Open File Dialog MVVM 的这段代码:
public interface IOpenFileService
{
string FileName { get; }
bool OpenFileDialog()
// Many other methods and properties of OpenFileDialog here...
}
但是,这意味着我必须实现 OpenFileDialog 的所有属性和方法,并让它们成为调用真正 OpenFileDialog 的属性和方法的传递。
我希望做一些事情,比如拥有一个 MockContainer 和一个 RealContainer,每个都会返回他们的 OpenFileDialog 版本:
public class MockContainer
{
IOpenFileDialog FileDialog { get { return new MockOpenFileDialog(); } }
}
public class RealContainer
{
IOpenFileDialog FileDialog { get { return new OpenFileDialog(); } }
}
但是,我不能这样做,因为它们没有实现通用接口。如果我能够采用这种方法,我就不需要在 IOpenFileService 中为 OpenFileDialog 所需的所有内容创建传递方法。每个容器只会返回一个调用者可以使用的对话框。
有没有一种方法可以使这种方法发挥作用,或者 IOpenFileService 真的可以做到吗?
注意:我了解模拟框架。我今天想快速实现一些东西,还不想花时间学习一个模拟框架。我想我可以很容易地自己模拟它。
【问题讨论】:
-
是的。我想过子类化它,但不能......