问题描述
目前,我正在努力学习正确的单元测试。所以现在我试图写单元测试应该从一个XML文件到适当的对象映射数据的类。当然类的所有功能是依赖于相应的XML文件的存在。该XML文件被加载在类的构造函数。
I'm currently trying to learn proper unit-test. So now I'm trying to write unit-tests for a class that should map data from an XML-File to the proper objects. Of course all functionality of the class is dependent on the existence of the corresponding XML-file. The XML-file is loaded in the constructor of the class.
我使用C#与NUnit的。到目前为止,我有两个测试:
I'm using C# with NUnit. So far I've got two tests:
[Test]
public void ShouldAllowInstanceToBeCreatedWhenXMLFileIsPresent()
{
if (File.Exists(SettingsReader.XML_SETTINGS_PATH))
{
SettingsReader settingsReader = new SettingsReader();
Assert.AreNotEqual(null, settingsReader);
}
}
[Test]
[ExpectedException("Telekanzlei.Clientmanager.XMLDataLayer.XMLFileNotFoundException")]
public void ShouldThrowExceptionWhenXMLFileIsNotPresent()
{
if (!File.Exists(SettingsReader.XML_SETTINGS_PATH))
{
SettingsReader settingsReader = new SettingsReader();
}
else
throw new XMLFileNotFoundException();
}
如果检查文件是否存在的测试是我不知道一种合适的方式去的,所以对这些测试任何建议,也欢迎。但我的问题是,如何与下面的测试继续进行。显然所有以下测试要失败,如果XML文件是不存在。
I'm not sure if checking the existence of the file in the test is a proper way to go, so any suggestions on those test are welcome too. But my question is, how to proceed with the following tests. Obviously all following tests are going to fail, if the XML-file is not present.
所以我假设XML的文件存在,同时牢记,那一个失败的测试可能只是意味着它是不是?这似乎也不会我的权利。
So do I assume that the XML-file is present, while keeping in mind, that a failing test could just mean that it's not? That wouldn't seem right to me.
有一个通用的模式,来处理这样吗?
Is there a general pattern, to handle a problem like this?
THX任何帮助。
编辑:改写了第二次测试,因为它是失败的,如果该文件是实际存在...
edit: rewrote the second test, as it was failing if the file was actually present...
EDIT2:它可以帮助告诉你,什么SettingsReader实际执行。到目前为止,它看起来像这样:
edit2: May it is helping to tell you, what the SettingsReader actually does. So far it looks like this:
public class SettingsReader
{
public static readonly string XML_SETTINGS_PATH = "C:\\Telekanzlei\\Clientmanager_2.0\\Settings.xml";
public XElement RootXElement { get; private set; }
public SettingsReader()
{
if (!File.Exists(XML_SETTINGS_PATH))
throw new XMLFileNotFoundException();
using (var fs = File.OpenRead(XML_SETTINGS_PATH))
{
RootXElement = XElement.Load(fs);
}
}
}
我不知道,但我想一个StreamReader不会去这里的路,不是吗?
I'm not sure, but I guess a StreamReader wouldn't be the way to go here, would it?
推荐答案
这个问题是不是与你的单元测试,但与类的设计。我建议重构类,所以它不会打开文件,而是在一个流上进行操作。然后,你的单元测试可以简单地取代内存流文件流 - simples! :)
The problem is not with your unit tests but with the design of the class. I'd suggest refactoring the class so it doesn't open the file but instead operates on a stream. Then your unit tests could simply replace a file stream for a memory stream - simples! :)
public class SettingsReader()
{
public SettingsReader(System.IO.StreamReader reader)
{
// read contents of stream...
}
}
// In production code:
new SettingsReader(new StreamReader(File.Open("settings.xml")));
// In unit test:
new SettingsReader(new StringReader("<settings>dummy settings</settings>"));
记住,打开文件和解析设置数据是两个非常不同的关注点。
Remember, opening a file and parsing settings data are two very different concerns.
这篇关于如何单元测试,需要一个特定的文件存在一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!