1

我的任务是找出当 xsd 部分中的属性与单元测试中的值不匹配时程序通过 XML 单元测试的原因。问题是我对 XML 或 xsd 几乎一无所知。我在各个地方都阅读过它,但没有任何东西能帮助我理解测试失败的原因。

似乎在我弄清楚它为什么不起作用之前,我需要了解进行单元测试的方法中发生了什么。我想我掌握了大部分内容,但有一部分我不确定我是否理解。这部分还处理 xsd。

这是我的xsd:

public static class Constants
{
    public const string Xsd = @"
<xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
   <xs:element name=""Datas"">
      <xs:complexType>
         <xs:sequence>
           <xs:element name=""Data"">
             <xs:attributeGroup ref=""allocData""/>
           </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>

   <xs:attributeGroup id=""allocData"">
     <xs:attribute name=""Site"" type=""string""/>    
     <xs:attribute name=""MonthNum"" type=""string""/>
     <xs:attribute name=""Department"" type=""string""/>
     <xs:attribute name=""Numerator"" type=""float""/>    
     <xs:attribute name=""Data_Indicator"" type=""string""/>
     <xs:attribute name=""Industry_Benchmark"" type=""int""/>
     <xs:attribute name=""OSHA_Benchmark"" type=""int""/>    
     <!--xs:attribute name=""Comments"" type=""string""/-->
     <xs:attribute name=""Executive_Comments"" type=""string""/>
     <xs:attribute name=""Fleet_Executive_Comments"" type=""string""/>
   </xs:attributeGroup>   
</xs:schema>";

这是我的单元测试的方法:

public void XMLTest()
{
    List<DataResults> ListOfResults = new List<DataResults>();

        DataResults r1 = new DataResults();
        DataResults r2 = new DataResults();
        HarvesterHandle testData = new HarvesterHandle();

        r1.Site = "Springfield Site";
        r1.Department = "Freak";
        r1.MonthNum = "8";
        r1.Numerator = 65807;
        r1.Data_Indicator = "A12";
        // r1.Comments = "Hello, World";
        r1.Industry_Benchmark = 23;
        r1.OSHA_Benchmark = 2;
        r1.ExecutiveComments = "It costs HOW MUCH?";
        r1.FleetExecComments = "No you can't have a new car";

        r2.Site = "Joplin Site";
        r2.Department = "Rock";
        r2.MonthNum = "12";
        r2.Numerator = 65625;
        r2.Data_Indicator = "Ou812";
        r2.Industry_Benchmark = 523;
        r2.OSHA_Benchmark = 2456;
        // r2.Comments = "GoodBye, World!";
        r2.ExecutiveComments = "Cut it!";
        r2.FleetExecComments = "It only has 250,000 miles";

        ListOfResults.Add(r1);
        ListOfResults.Add(r2);

        var test = testData.XMLTest(ListOfResults);


        var xmlDoc = XDocument.Parse(test.ToString());

        XmlSchemaSet set = new XmlSchemaSet();
        set.Add(XmlSchema.Read(XElement.Parse(Constants.Xsd).CreateReader(), (o, e) => { })); bool valid = true;
        xmlDoc.Validate(set, (o, e) =>
        {
            valid = false;
        });
        Assert.IsTrue(valid);

        var element = test.Descendants("Data").FirstOrDefault(x => x.Attribute("Site").Value.Equals(r1.Site));

        Assert.IsNotNull(element);

        Assert.AreEqual(r1.Site, element.Attribute("Site").Value);
        Assert.AreEqual(r1.Department, element.Attribute("Department").Value);
        Assert.AreEqual(r1.MonthNum.ToString(), element.Attribute("MonthNum").Value);
        Assert.AreEqual(r1.Numerator.ToString(), element.Attribute("Numerator").Value);
        Assert.AreEqual(r1.Data_Indicator, element.Attribute("Data_Indicator").Value);
        Assert.AreEqual(r1.Industry_Benchmark.ToString(), element.Attribute("Industry_Benchmark").Value);
        Assert.AreEqual(r1.OSHA_Benchmark.ToString(), element.Attribute("OSHA_Benchmark").Value);
        // Assert.AreEqual(r1.Comments.ToString(), element.Attribute(
        //     "Comments").Value);
        Assert.AreEqual(r1.ExecutiveComments.ToString(), element.Attribute("Executive_Comments").Value);
        Assert.AreEqual(r1.FleetExecComments.ToString(), element.Attribute("Fleet_Executive_Comments").Value);

        element = test.Descendants("Data").FirstOrDefault(x => x.Attribute("Site").Value.Equals(r2.Site));

        Assert.IsNotNull(element);

        Assert.AreEqual(r2.Site, element.Attribute("Site").Value);
        Assert.AreEqual(r2.Department, element.Attribute("Department").Value);
        Assert.AreEqual(r2.MonthNum.ToString(), element.Attribute("MonthNum").Value);
        Assert.AreEqual(r2.Numerator.ToString(), element.Attribute("Numerator").Value);
        Assert.AreEqual(r2.Data_Indicator, element.Attribute("Data_Indicator").Value);
        Assert.AreEqual(r2.Industry_Benchmark.ToString(), element.Attribute("Industry_Benchmark").Value);
        Assert.AreEqual(r2.OSHA_Benchmark.ToString(), element.Attribute("OSHA_Benchmark").Value);
        // Assert.AreEqual(r2.Comments.ToString(), element.Attribute(
        //    "Comments").Value);
        Assert.AreEqual(r2.ExecutiveComments.ToString(), element.Attribute(Executive_Comments").Value);
        Assert.AreEqual(r2.FleetExecComments.ToString(), element.Attribute("Fleet_Executive_Comments").Value);
    }

似乎正在处理 xsd 的部分是这样的:

XmlSchemaSet set = new XmlSchemaSet();
set.Add(XmlSchema.Read(XElement.Parse(Constants.Xsd).CreateReader(), (o, e) => { })); bool valid = true;
xmlDoc.Validate(set, (o, e) =>
{
    valid = false;
});
    Assert.IsTrue(valid);

问题是如果 xsd 中的属性数据类型与实际数据不匹配,测试仍然通过。例如,如果任何字符串属性(MonthNum、Site 等)更改为浮动,测试仍然通过。此外,如果 .xsd 元素名称 Datas 和 Data 更改为 Fred 和 Ethyl 之类的名称,则测试仍然通过。

我假设无论 xml 单元测试方法中代码的 xsd 部分在做什么,它都不会检查正确的数据类型或元素名称

每个请求,这是我的 xml:

    public const string XSLT = @"
<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">
  <xsl:output method=""text"" encoding=""us-ascii"" />
  <xsl:template match=""/"">Site, MonthNum, Department, Numerator, Denominator, Data_Indicator, Industry_Benchmark, OSHA_Benchmark, <!--Comments--> Executive_Comments, Fleet_Executive_Comments,
  <xsl:for-each select=""Datas/Data"">
  <xsl:text>'</xsl:text>
  <xsl:value-of select=""@site""/>
  <xsl:text>','</xsl:text>      
  <xsl:value-of select=""@monthNum""/>
  <xsl:text>','</xsl:text>
  <xsl:value-of select=""@Department""/>
  <xsl:text>','</xsl:text>
  <xsl:value-of select=""@numerator""/>
  <xsl:text>','</xsl:text>,       
  <xsl:value-of select=""Industry_Benchmark""/>
  <xsl:text>'</xsl:text>,
  <xsl:value-of select=""OSHA_Benchmark""/>
  <xsl:text>'</xsl:text>,
  <!--xsl:value-of select=""Comments""/-->
  <!--xsl:text>'</xsl:text-->,
  <xsl:value-of select=""Executive_Comments""/>
  <xsl:text>'</xsl:text>,
  <xsl:value-of select=""Fleet_Executive_Comments""/>
  <xsl:text>'</xsl:text>,

  </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>";
}

}

编辑:这是运行测试时生成的 XML:

<Data>
  <Data Site="Springfield Site" MonthNum="8" Department="Freak" Numerator="65807" Data_Indicator="A12" Industry_Benchmark="23" OSHA_Benchmark="2" Executive_Comments="It costs HOW MUCH?" Fleet_Executive_Comments="No you can't have a new car" />
  <Data Site="Joplin Site" MonthNum="12" Department="Rock" Numerator="65625" Data_Indicator="Ou812" Industry_Benchmark="523" OSHA_Benchmark="2456" Executive_Comments="Cut it!" Fleet_Executive_Comments="It only has 250,000 miles" />
</Data>
4

1 回答 1

0

看来问题在于 xsd 不正确。它被写成:

<xs:attribute name=""Site"" type=""string""/>  

什么时候应该写成

<xs:attribute name=""Site"" type=""xs:string""/>  
于 2012-06-28T18:53:02.473 回答