MS 单元测试中出现异常?
我为我的项目的方法创建了一个单元测试。当找不到文件时,该方法会引发异常。我为此编写了一个单元测试,但是当引发异常时我仍然无法通过测试。
方法是
public string[] GetBuildMachineNames(string path)
{
string[] machineNames = null;
XDocument doc = XDocument.Load(path);
foreach (XElement child in doc.Root.Elements("buildMachines"))
{
int i = 0;
XAttribute attribute = child.Attribute("machine");
machineNames[i] = attribute.Value;
}
return machineNames;
}
单元测试
[TestMethod]
[DeploymentItem("TestData\\BuildMachineNoNames.xml")]
[ExpectedException(typeof(FileNotFoundException),"Raise exception when file not found")]
public void VerifyBuildMachineNamesIfFileNotPresent()
{
var configReaderNoFile = new ConfigReader();
var names = configReaderNoFile.GetBuildMachineNames("BuildMachineNoNames.xml");
}
我应该处理方法中的异常还是我遗漏了其他东西?
编辑:
我传递的路径不是找到该文件的路径,因此该测试应该通过...即,如果该路径中不存在文件怎么办。
I created a unit test for a method of my project. That method raises an exception when a file is not found. I wrote a unit test for that, but I'm still not able to pass the test when the exception is raised.
Method is
public string[] GetBuildMachineNames(string path)
{
string[] machineNames = null;
XDocument doc = XDocument.Load(path);
foreach (XElement child in doc.Root.Elements("buildMachines"))
{
int i = 0;
XAttribute attribute = child.Attribute("machine");
machineNames[i] = attribute.Value;
}
return machineNames;
}
Unit Test
[TestMethod]
[DeploymentItem("TestData\\BuildMachineNoNames.xml")]
[ExpectedException(typeof(FileNotFoundException),"Raise exception when file not found")]
public void VerifyBuildMachineNamesIfFileNotPresent()
{
var configReaderNoFile = new ConfigReader();
var names = configReaderNoFile.GetBuildMachineNames("BuildMachineNoNames.xml");
}
Should I handle the Exception in the method or am I missing something else??
EDIT:
The path I am passing is not the one to find the file, so this test should pass... i.e. what if file not exists in that path.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的单元测试中,您似乎正在部署一个 xml 文件:
TestData\BuildMachineNoNames.xml
,并将其传递给GetBuildMachineNames
。因此该文件存在,并且您不能期望抛出FileNotFoundException
。所以也许像这样:In your unit test it seems that you are deploying an xml file:
TestData\BuildMachineNoNames.xml
which you are passing to theGetBuildMachineNames
. So the file exists and you cannot expect aFileNotFoundException
to be thrown. So maybe like this:通过放置 [ExpectedException(typeof(FileNotFoundException),"Raise exception when file not find")] 属性,您期望该方法将抛出 FileNotFoundException,如果未抛出 FileNotFoundException 测试将失败。否则测试就会成功。
By putting [ExpectedException(typeof(FileNotFoundException),"Raise exception when file not found")] attribute you are expecting that the method will throw an FileNotFoundException, if the FileNotFoundException not thrown Test will fail. Otherwise Test will be success.
我从来没有真正理解
ExpectedException
的意义。您应该能够捕获代码中的异常,而不是属性中的异常。这是一种更好的做法,并且还允许您在引发后执行一些操作(例如更多验证)...此外,它还可以让您在调试器中停止代码并检查内容,而不需要在论坛中询问。 :)我会使用 Assert.Throws( TestDelegate code );.
请参阅此处的示例。
I never really understood the point of
ExpectedException
. You should be able to catch exception in code rather than in attributes. It is a better practice and also allows you to do stuff after it is raised (e.g. more validations)... Also it would let you stop the code in debugger and check things out rather than need to ask in forums. :)I'd use Assert.Throws( TestDelegate code );.
See here an example.