需要:文件系统接口和执行.NET [英] Needed: File system interfaces and implementation in .NET

查看:247
本文介绍了需要:文件系统接口和执行.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/1087351/how-do-you-mock-out-the-file-system-in-c-sharp-for-unit-testing">How你模拟出在C#中的文件系统进行单元测试?

我编写单元测试到我的code,使用起订量为模拟框架。
我的code包括调用文件系统,使用直接调用 System.IO 类。例如, File.Exists(...)
我想改变这种状况code更可检验的,所以我应该有一个接口,比如的IFile ,与相关法,说存在(字符串路径)
我知道我可以从头开始写,但我想,也许有一个完整的,可靠的框架,同时具有接口和实现文件系统。这(需要)框架,也可能是某种形式的服务,因此,其API并没有成为一个接口等价于 System.IO 命名空间。照片 请注意,我真的很想有接口(和不可以静态方法),使我的code是准备依赖注入

I write unit tests to my code, using Moq as a mocking framework.
My code includes calls to the file system, using direct calls to System.IO classes. For instance, File.Exists(...) etc.
I'd like to change that code to be more testable, so I should have an interface, say IFile, with a relevant method, say Exists(string path).
I know I can write it from scratch, but I thought that maybe there's a complete, robust framework that has both interfaces and implementations for the file system. This (desired) framework may also be some kind of a "service", and therefore its API doesn't have to be an "interface equivalent" to the System.IO namespace.
Note that I'd really like to have interfaces (and not static methods) so that my code is ready for dependency injection

  • 系统莫名其妙地相似,但<一个href="http://stackoverflow.com/questions/266776/mock-file-methods-in-net-like-file-copy1-txt-2-txt">not同样的问题被要求在计算器
  • codeplex.com 有一个名为项目的 codePLEX源代码控制客户端的(链接),有这样的类在源code(请参阅 /来源/ TfsLibrary /工具/ 源$ C ​​$ C的具体细节)
  • A somehow similar, yet not the same question was asked in stackoverflow
  • In codeplex.com there's a project named CodePlex Source Control Client (link) that has such classes in the source code (see /Source/TfsLibrary/Utility/ in the source code for specific details)

任何其他建议?

推荐答案

我写适配器,在静态有System.IO.File 目录的方法。然后,在我的课我做到以下几点:

I've written adapters for the static System.IO.File and Directory methods. Then in my classes I do the following:

public class MyService {
  public IFile File {private get;set;}
  public MyService() {
    File = new FileImpl();
  }
  public void DoSomething() {
    File.ReadAllText("somefile");
  }
}

然后就可以进行测试注入模拟为的IFile。

Then you can inject a mock as IFile for testing.

这篇关于需要:文件系统接口和执行.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆