MS 单元测试中出现异常?

发布于 2024-10-16 06:06:13 字数 989 浏览 4 评论 0原文

我为我的项目的方法创建了一个单元测试。当找不到文件时,该方法会引发异常。我为此编写了一个单元测试,但是当引发异常时我仍然无法通过测试。

方法是

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和影子一齐双人舞 2024-10-23 06:06:13

在您的单元测试中,您似乎正在部署一个 xml 文件:TestData\BuildMachineNoNames.xml,并将其传递给 GetBuildMachineNames。因此该文件存在,并且您不能期望抛出 FileNotFoundException 。所以也许像这样:

[TestMethod]
[ExpectedException(typeof(FileNotFoundException), "Raise exception when file not found")]
public void VerifyBuildMachineNamesIfFileNotPresent()
{
    var configReaderNoFile = new ConfigReader();
    var names = configReaderNoFile.GetBuildMachineNames("unexistent.xml");
}

In your unit test it seems that you are deploying an xml file: TestData\BuildMachineNoNames.xml which you are passing to the GetBuildMachineNames. So the file exists and you cannot expect a FileNotFoundException to be thrown. So maybe like this:

[TestMethod]
[ExpectedException(typeof(FileNotFoundException), "Raise exception when file not found")]
public void VerifyBuildMachineNamesIfFileNotPresent()
{
    var configReaderNoFile = new ConfigReader();
    var names = configReaderNoFile.GetBuildMachineNames("unexistent.xml");
}
幸福丶如此 2024-10-23 06:06:13

通过放置 [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.

给妤﹃绝世温柔 2024-10-23 06:06:13

我从来没有真正理解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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文