我们有一些调用File.Copy,File.Delete,File.Exists等的方法.如何在不实际访问文件系统的情况下测试这些方法?
我认为自己是一个单元测试n00b,所以任何建议都表示赞赏.
我想在单元测试中加载外部XML文件,以测试该XML上的一些处理代码.如何获取文件的路径?
通常在Web应用程序中我会这样做:
XDocument.Load(Server.MapPath("/myFile.xml"));
Run Code Online (Sandbox Code Playgroud)
但显然在我的单元测试中我没有引用Server或HttpContext,那么如何映射路径以便我不必指定完整路径?
更新:
我只想说明我实际测试的代码是针对XML解析器类的,类似于:
public static class CustomerXmlParser {
public static Customer ParseXml(XDocument xdoc) {
//...
}
}
Run Code Online (Sandbox Code Playgroud)
所以为了测试这个,我需要解析一个有效的XDocument.正在测试的方法不会访问文件系统本身.我可以直接在测试代码中从String创建XDocument,但我认为从文件加载它会更容易.
可能重复:
如何在C#中模拟文件系统进行单元测试?
我使用Moq作为模拟框架,将单元测试编写到我的代码中.
我的代码包括使用对System.IO类的直接调用来调用文件系统.例如,File.Exists(...)等等.
我想将该代码更改为更易测试,所以我应该有一个接口,比方说IFile,用相关的方法Exists(string path).
我知道我可以从头开始编写它,但我认为可能有一个完整,健壮的框架,它具有文件系统的接口和实现.这个(期望的)框架也可以是某种"服务",因此它的API不必是System.IO命名空间的"接口等价物" .
请注意,我真的很想拥有接口(而不是静态方法),以便我的代码可以用于依赖注入
/Source/TfsLibrary/Utility/具体细节参见源代码) 还有其他建议吗?
我试图在C#中模拟出System.net.Sockets.Socket类 - 我尝试使用NUnit模拟但它不能模拟具体的类.我也尝试过使用Rhino Mocks,但它似乎使用了该类的真实版本,因为它在调用Send(byte [])时抛出了SocketException.有没有人使用任何模拟框架成功创建和使用Socket模拟?
I created an extension method to add all JSON configuration files to the IConfigurationBuilder
public static class IConfigurationBuilderExtensions
{
public static IConfigurationBuilder AddJsonFilesFromDirectory(
this IConfigurationBuilder configurationBuilder,
IFileSystem fileSystem,
string pathToDirectory,
bool fileIsOptional,
bool reloadConfigurationOnFileChange,
string searchPattern = "*.json",
SearchOption directorySearchOption = SearchOption.AllDirectories)
{
var jsonFilePaths = fileSystem.Directory.EnumerateFiles(pathToDirectory, searchPattern, directorySearchOption);
foreach (var jsonFilePath in jsonFilePaths)
{
configurationBuilder.AddJsonFile(jsonFilePath, fileIsOptional, reloadConfigurationOnFileChange);
}
return configurationBuilder;
}
}
Run Code Online (Sandbox Code Playgroud)
and want to create tests for it using xUnit. Based on
到目前为止,我创建了以下界面:
public interface IDirectoryInfoWrapper
{
public IFileInfoWrapper[] GetFiles(string searchPattern, SearchOption searchType);
public IDirectoryInfoWrapper[] GetDirectories();
}
Run Code Online (Sandbox Code Playgroud)
我一直在使用代码替换DirectoryInfo IDirectoryInfoWrapper.一切顺利,直到我发现这个:
// Check that the directory is valid
DirectoryInfo directoryInfo = new DirectoryInfo( argPath );
if ( directoryInfo.Exists == false )
{
throw new ArgumentException
("Invalid IFileFinder.FindFiles Directory Path: " + argPath);
}
Run Code Online (Sandbox Code Playgroud)
将构造函数放在接口中是没有意义的,所以我应该如何处理这行代码:
DirectoryInfo directoryInfo = new DirectoryInfo( argPath );
Run Code Online (Sandbox Code Playgroud) 我正在使用Moq框架进行单元测试.
我有一个描述类的接口,如下所示:
public interface ISourceFileLocation : IFileLocation, IDisposable
{
bool RemoveAfterTransfer { get; set; }
void RemoveSource();
//.....
}
Run Code Online (Sandbox Code Playgroud)
为了确保调用RemoveSource-Method,我实现了一个基类,其中在dispose方法中调用remove.
public abstract class SourceFileBase : ISourceFileLocation
{
//......
public bool RemoveAfterTransfer { get; set; }
public void RemoveSource()
{
if (File.Exists(Uri.AbsolutePath))
{
File.Delete(Uri.AbsolutePath);
}
}
public void Dispose()
{
if (this.RemoveAfterTransfer)
this.RemoveSource();
}
}
Run Code Online (Sandbox Code Playgroud)
模拟ISourceFileLocation时没有默认实现,因此对于测试我想在具体类应该继承的基类上实现测试.
在模拟基类时,我的测试期望RemoveSource-Method是虚拟的,这打破了我确保调用方法的想法!
这是缺乏框架,有更好的框架或方法来测试这个,或者这是我的代码的问题,我应该重新考虑我的设计?
亲切的问候.
测试方法:
[TestCategory("Source")]
[TestMethod]
public void CheckIfRemoveSourceMethodIsCalled()
{
//ARRANGE
var mockSourceLocation = new Mock<SourceFileBase>();
mockSourceLocation.Object.RemoveAfterTransfer = true;
mockSourceLocation.Setup(x => x.RemoveSource());
//ACT
mockSourceLocation.Object.Dispose();
/ASSERT …Run Code Online (Sandbox Code Playgroud) 我正在开发一个简单的项目,作为TDD中的练习而不是其他任何东西.该程序从Web服务器获取一些图像并将其保存为文件.为了记录,我正在做的(我期望的最终结果)与这个perl脚本非常相似,但在C#中.
我已经到了需要将文件保存到磁盘的地步.我需要进行单元测试来强制执行代码.我不知道如何处理这个问题.我希望能够验证代码是否创建了具有预期文件名的预期文件,当然我根本不想触摸文件系统.我并不是单元测试和TDD的新手,但出于某种原因,我真的不清楚在这种情况下该怎么做.我确信,一旦我看到它,答案就会很明显,但......我脑中一个代码来自的神秘地方就是不合作.
我选择的工具是MSpec和FakeItEasy,但感谢任何框架中的建议.什么是单元测试文件系统交互的明智方法?
c# ×6
mocking ×4
unit-testing ×4
.net ×2
.net-2.0 ×1
file-io ×1
moq ×1
nunit ×1
nunit-mocks ×1
rhino-mocks ×1
tdd ×1