繁体   English   中英

单元测试Linq XML函数

[英]Unit Test Linq XML Function

我有下一个函数,该函数根据文档中的数据创建一个列表。

我需要进行一些单元测试,但是不知道如何针对此特定功能进行实际测试。 我到处都读过,但还是没有运气。

public List<Info> getInfo()
{
    XDocument doc = loadDocument();

    var variable = (from elem in doc.Descendants("Information").Elements()
                   select new Info
                   {
                       Include = elem.Element("Include") != null && (elem.Element("Include").Value.Equals("true") || elem.Element("Include").Value.Equals("false")) ? Convert.ToBoolean(elem.Element("Include").Value) : false,
                       InfoName = elem.Element("Name") != null ? elem.Element("Name").Value : String.Empty,
                       StartDate = elem.Element("StartDate") != null ? elem.Element("StartDate").Value : String.Empty,
                       EndDate = elem.Element("EndDate") != null ? elem.Element("EndDate").Value : String.Empty,
                       Mark = elem.Element("Mark") != null ? Convert.ToDouble(elem.Element("Mark").Value) : Double.NaN
                   }
                   ).ToList();

    return variable;
}

public XDocument loadDocument() 
{ 
    XDocument doc = XDocument.Load("info.xml"); 
    return doc; 
}

我认为您需要做的是创建一个单元测试。

[TestMethod]
public void CheckMyData()
{
    var _info = GenerateData();
    Assert.IsTrue(_info.Include == true); //Mock condition to fulfill.
    Assert.IsFalse(_info.InfoName.Contains("test1_Name"));
    //... and so on...
    //You can also check for the expected type of values.
}

private Info GenerateData()
{
    var data = new Info
        {
          //We set some mokup values here
          Include = true,
          InfoName = "test_NAME",
          StartDate = "12-12-2005",
          EndDate = "14/12/2005",
          Mark = 12
        }
    return data;
}

正是您所需要的。 该模拟框架具有模拟LinQ函数的独特功能。 您还可以将代码示例发送给他们的支持,他们将指导您正确执行此操作

暂无
暂无

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

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