I created a unit test for my project method. This method throws an exception when the file is not found. I wrote a unit test for this, but I still cannot pass the test when an exception occurs.
Method
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 pass is not the one to find the file, so this test should pass ... that is, if the file does not exist in that path.
source share