简体   繁体   English

在单元测试用例中捕获引发的异常的问题

[英]Issue in catching the thrown exception in Unit Test Case

I have a method that takes an argument Source Folder / Source File.I have handled the code in such a way that if Source Folder or Source File does not exist , it should throw DirectoryNotFound Exception or FileNotFound Exception accordingly. 我有一个采用参数Source Folder / Source File的方法。我已经以某种方式处理代码,如果Source Folder或Source File不存在,它将相应地抛出DirectoryNotFound Exception或FileNotFound Exception。 Following is the Code Snippet 以下是代码段

          Boolean isSourceExist = Directory.Exists(sourceFileorFolder);
          Boolean isFileExist = File.Exists(sourceFileorFolder);
            if (!(
                ((isSourceExist == true) && (isFileExist == false)) ||
                ((isSourceExist == false) && (isFileExist == true))
               ))
            {
                if (isSourceExist == false)
                    throw new DirectoryNotFoundException();
                else if (isFileExist == false)
                    throw new FileNotFoundException();
            }

While trying to unit test this method for negative scenario ie providing a folder that does not exist, [ExpectedException(typeof(DirectoryNotFoundException))] fails in unit test case. 尝试对否定方案(例如,提供一个不存在的文件夹)对该方法进行单元测试时,[ExpectedException(typeof(DirectoryNotFoundException))]在单元测试案例中失败。 But the actual code responds properly by throwing the appropriate exceptions based on the inputs. 但是实际代码会根据输入抛出适当的异常,从而正确响应。

Thanks in advance 提前致谢

Normally, you could write two distinct tests, one for the file case, and one for the directory case. 通常,您可以编写两个不同的测试,一个用于文件大小写,另一个用于目录大小写。

Anyway, this code does very likely not behave as you expect. 无论如何,此代码很可能不会像您期望的那样运行。 The code snippet you posted will always throw if either a file or a directory exists at sourceFileorFolder path. 如果在sourceFileorFolder路径上存在文件或目录,则您发布的代码片段将始终抛出。

If the path exists as a file, you throw DirectoryNotFoundException , else you throw a FileNotFoundException . 如果路径以文件形式存在,则抛出DirectoryNotFoundException ,否则抛出FileNotFoundException Only if the file does not exist you proceed without throwing. 仅当文件不存在时,您才继续进行而不抛出。

EDIT: I missed the ! 编辑:我错过了! in the big if statement. 在大if语句中。 Actually this method never throws, because the first if statement is entered only if the file does not exist ( not ((folder and not file) or (file and not a folder)) is the same as not (file or folder) ). 实际上,此方法永远不会抛出异常,因为只有在文件不存在时才输入第一个if语句( 不((文件夹和非文件)或(文件而不是文件夹))不(文件或文件夹)相同 )。 The smaller if statements behave as I have written above. 如我上面所写,较小的if语句的行为。

no need to go this complex, remove the upper if condition 无需复杂,去除上层的情况

Boolean isSourceExist = Directory.Exists(sourceFileorFolder);
Boolean isFileExist = File.Exists(sourceFileorFolder);

if (isSourceExist == false)
     throw new DirectoryNotFoundException();
else if (isFileExist == false)
     throw new FileNotFoundException();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM