0
如何測試Xml文件更新程序方法?單元測試LINQ to Xml方法
public static void AllowComp(string Name)
{
var xml = XDocument.Load(XmlPath);
var comp = xml.Descendants("components").Single(c => c.Attribute("key").Value == Name);
comp.Attribute("allow").Value = "yes";
xml.Save(XmlPath);
}
謝謝
你不能單元測試該方法。首先,它做了很多事情,其次它有副作用。當然兩者都有關係。分離出方法的文件系統部分,你將得到一個純函數,它接受一個XDoc並返回一個XDoc。這將是可測試的。 – Aron
做喲意味着我需要另一種方法嗎? public XDocument Update(XDocument file,string Name) var comp = xml.Descendants(「components」)。Single(c => c.Attribute(「key」)。Value == Name); comp.Attribute(「allow」)。Value =「yes」; 返回文件; } – user3409988
然後第一個方法將是公共靜態void AllowComp(string Name) {0} xml = XDocument.Load(XmlPath); var XDoc = Update(xml,Name); xml.Save(XmlPath); } – user3409988