当前位置:  开发笔记 > 编程语言 > 正文

.NET中的模拟文件方法(如File.Copy("1.txt","2. txt"))

如何解决《.NET中的模拟文件方法(如File.Copy("1.txt","2.txt"))》经验,为你挑选了1个好方法。

我们有一些调用File.Copy,File.Delete,File.Exists等的方法.如何在不实际访问文件系统的情况下测试这些方法?

我认为自己是一个单元测试n00b,所以任何建议都表示赞赏.



1> yfeldblum..:
public interface IFile {
    void Copy(string source, string dest);
    void Delete(string fn);
    bool Exists(string fn);
}

public class FileImpl : IFile {
    public virtual void Copy(string source, string dest) { File.Copy(source, dest); }
    public virtual void Delete(string fn) { File.Delete(fn); }
    public virtual bool Exists(string fn) { return File.Exists(fn); }
}

[Test]
public void TestMySystemCalls() {
    var filesystem = new Moq.Mock();
    var obj = new ClassUnderTest(filesystem);
    filesystem.Expect(fs => fs.Exists("MyFile.txt")).Return(true);
    obj.CheckIfFileExists(); // doesn't hit the underlying filesystem!!!
}

推荐阅读
雯颜哥_135
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有