对于我们.Net程序员,System.Web.Abstractions我们都非常熟悉,主要作用于Web可以实现单元测试,他是在.Net framework 3.5 sp1开始引入的,很好的解决项目表示层不好做单元测试的问题,这个库所有类都是Wrapper/Decorator模式的。今天给推荐一个IO的扩展库与System.Web.Abstractions一样的,用来支持IO实现单元测试功能。
一个支持IO实现单元测试的扩展库,支持跨平台,与File所有API接口都一样,方便我们项目扩展、迁移。

项目主要核心文件是IFileSystem和FileSystem。
技术架构
1、平台:基于Net4、Netstandard2.0开发
2、开发工具:Visual Studio 2017
使用方法
读取文本使用例子,使用方法与File类一样,都是使用相同API名ReadAllText,只是这个API支持可注入和可测试的。
public class MyComponent{readonly IFileSystem fileSystem;// <summary>Create MyComponent with the given fileSystem implementation</summary>public MyComponent(IFileSystem fileSystem) {this.fileSystem = fileSystem; }/// <summary>Create MyComponent</summary>public MyComponent() : this( fileSystem: new FileSystem() //use default implementation which calls System.IO ) { }public void Validate() {foreach (var textFile in fileSystem.Directory.GetFiles(@"c:\", "*.txt", SearchOption.TopDirectoryOnly)) {var text = fileSystem.File.ReadAllText(textFile);if (text != "Testing is awesome.")throw new NotSupportedException("We can't go on together. It's not me, it's you."); } }}
文件创建单元测试
[Test]public void Mockfile_Create_ShouldCreateNewStream() {string fullPath = XFS.Path(@"c:\something\demo.txt");var fileSystem = new MockFileSystem(); fileSystem.AddDirectory(XFS.Path(@"c:\something"));var sut = new MockFile(fileSystem); Assert.That(fileSystem.FileExists(fullPath), Is.False); sut.Create(fullPath).Dispose(); Assert.That(fileSystem.FileExists(fullPath), Is.True); }
删除文件单元测试
[Test]public void MockFile_Delete_ShouldDeleteFile() {var fileSystem = new MockFileSystem();var path = XFS.Path("C:\\test");var directory = fileSystem.Path.GetDirectoryName(path); fileSystem.AddFile(path, new MockFileData("Bla"));var fileCount1 = fileSystem.Directory.GetFiles(directory, "*").Length; fileSystem.File.Delete(path);var fileCount2 = fileSystem.Directory.GetFiles(directory, "*").Length; Assert.AreEqual(1, fileCount1, "File should have existed"); Assert.AreEqual(0, fileCount2, "File should have been deleted"); }
文件复制单元测试
[Test]public void MockFile_Copy_ShouldOverwriteFileWhenOverwriteFlagIsTrue() {string sourceFileName = XFS.Path(@"c:\source\demo.txt");var sourceContents = new MockFileData("Source content");string destFileName = XFS.Path(@"c:\destination\demo.txt");var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { {sourceFileName, sourceContents}, {destFileName, new MockFileData("Destination content")} }); fileSystem.File.Copy(sourceFileName, destFileName, true);var copyResult = fileSystem.GetFile(destFileName); Assert.AreEqual(copyResult.Contents, sourceContents.Contents); }
文件移动单元测试
[Test]public void MockFile_Move_ShouldMoveFileWithinMemoryFileSystem() {string sourceFilePath = XFS.Path(@"c:\something\demo.txt");string sourceFileContent = "this is some content";var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { {sourceFilePath, new MockFileData(sourceFileContent)}, {XFS.Path(@"c:\somethingelse\dummy.txt"), new MockFileData(new byte[] {0})} });string destFilePath = XFS.Path(@"c:\somethingelse\demo1.txt"); fileSystem.File.Move(sourceFilePath, destFilePath); Assert.That(fileSystem.FileExists(destFilePath), Is.True); Assert.That(fileSystem.GetFile(destFilePath).TextContents, Is.EqualTo(sourceFileContent)); Assert.That(fileSystem.FileExists(sourceFilePath), Is.False); }
支持 .NET Framework用法
FileInfo SomeBadApiMethodThatReturnsFileInfo(){return new FileInfo("a");}void MyFancyMethod(){var testableFileInfo = (FileInfoBase)SomeBadApiMethodThatReturnsFileInfo(); ...}
项目地址:https://github.com/Haydabase/System.IO.Abstractions
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当
我有一个围绕一些对象的包装类,我想将这些对象用作散列中的键。包装对象和解包装对象应映射到相同的键。一个简单的例子是这样的:classAattr_reader:xdefinitialize(inner)@inner=innerenddefx;@inner.x;enddef==(other)@inner.x==other.xendenda=A.new(o)#oisjustanyobjectthatallowso.xb=A.new(o)h={a=>5}ph[a]#5ph[b]#nil,shouldbe5ph[o]#nil,shouldbe5我试过==、===、eq?并散列所有无济于事。
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?