プロジェクトのメソッドの単体テストを作成しました。このメソッドは、ファイルが見つからない場合に例外を発生させます。そのための単体テストを作成しましたが、例外が発生した場合でもテストに合格できません。
方法は
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");
}
メソッドで例外を処理する必要がありますか、それとも何か他のものがありませんか?
編集:
私が渡しているパスはファイルを見つけるパスではないので、このテストは合格するはずです...つまり、ファイルがそのパスに存在しない場合はどうなりますか。