我有一个看起来像这样的课程:
Public Class TestResult
Public Sub New(labelName As String, correctValues As List(Of Object))
Me.LabelName = labelName
Me.SelectedValues = correctValues
End Sub
Public Sub New()
End Sub
Public Property LabelName As String
Public Property SelectedValues As List(Of Object)
End Class
我正在尝试将XML解析为List(Of TestResult)
。
问题是,根据执行的类型(测试或现场),每次都会得到不同的结果。通过实时运行给我预期的结果。
这是将XML解析为List(Of TestResult)
Public Function ReturnTestValidResults(testID As String) As IEnumerable(Of TestResult) Implements ITestManager.ReturnTestValidResults
Dim testValidResults = ReturnSingleRepComplianceFieldViaTestID("TestValidResults", testID)
Dim serializer = New XmlSerializer(GetType(List(Of TestResult)))
Dim result As List(Of TestResult)
Using reader = New StringReader(testValidResults)
result = serializer.Deserialize(reader)
End Using
Return result
End Function
这是XML:
<ArrayOfTestResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TestResult>
<LabelName>Question1Label</LabelName>
<SelectedValues>
<anyType xsi:type="xsd:int">2</anyType>
</SelectedValues>
</TestResult>
<TestResult>
<LabelName>Question2Label</LabelName>
<SelectedValues>
<anyType xsi:type="xsd:int">2</anyType>
</SelectedValues>
</TestResult>
<TestResult>
<LabelName>Question3Label</LabelName>
<SelectedValues>
<anyType xsi:type="xsd:int">3</anyType>
</SelectedValues>
</TestResult>
<TestResult>
<LabelName>Question4Label</LabelName>
<SelectedValues>
<anyType xsi:type="xsd:boolean">true</anyType>
</SelectedValues>
</TestResult>
</ArrayOfTestResult>
从单元测试运行代码时,它会正确解析为List(Of TestResult)
但是SelectedValues
属性包含XML类型和值的列表。而现场只是价值。
以下是一个例子:
单元测试结果:
{Attribute, Name="xsi:type", Value="xsd:Int"}
{Text, Value="2"}
住:
{2}
我唯一可以理解为什么会发生这种情况的原因是因为我必须将XML放在我的单元测试中,并使用双引号来阻止它转义字符串。
这是我的单元测试代码:
<TestMethod()> Public Async Function CheckSubmittedResults_SubmittedLabelsAreCorrect_ActionReturnsPassedJsonResult() As Task
Dim testController = New TestController(_iTestManager, _ihomeManager, _iGeneralACManager)
Dim submittedResults = New List(Of TestResult)
Dim validResults = New List(Of TestResult)
submittedResults.Add(New TestResult("Question1Label", New List(Of Object)() From {2}))
submittedResults.Add(New TestResult("Question2Label", New List(Of Object)() From {2}))
submittedResults.Add(New TestResult("Question3Label", New List(Of Object)() From {3}))
submittedResults.Add(New TestResult("Question4Label", New List(Of Object)() From {True}))
#Region "HugeXMLString"
Dim testResults = $"<ArrayOfTestResult xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><TestResult><LabelName>Question1Label</LabelName><SelectedValues><anyType xsi:type=""xsd:Int"">2</anyType></SelectedValues></TestResult><TestResult><LabelName>Question2Label</LabelName><SelectedValues><anyType xsi:type=""xsd: Int"">2</anyType></SelectedValues></TestResult><TestResult><LabelName>Question3Label</LabelName><SelectedValues><anyType xsi:type=""xsd: Int"">3</anyType></SelectedValues></TestResult><TestResult><LabelName>Question4Label</LabelName><SelectedValues><anyType xsi:type=""xsd: Boolean"">true</anyType></SelectedValues></TestResult></ArrayOfTestResult>"
#End Region
_iTestRepository.Setup(Function(x) x.ReturnSingleRepComplianceFieldViaTestID("TestValidResults", Nothing)).Returns(testResults)
Dim result = Await testController.CompleteTest(submittedResults, Nothing)
Assert.IsTrue(result.Data.Success = True, "Action should have passed.")
有人能看出为什么会这样吗?
答案 0 :(得分:0)
我用这个
替换了单元测试中的测试结果Dim testResults = String.Format("<ArrayOfTestResult xmlns:xsi='{0}' xmlns:xsd='{1}'><TestResult><LabelName>Question1Label</LabelName><SelectedValues><anyType xsi:type='{2}'>2</anyType></SelectedValues></TestResult><TestResult><LabelName>Question2Label</LabelName><SelectedValues><anyType xsi:type='{3}'>2</anyType></SelectedValues></TestResult><TestResult><LabelName>Question3Label</LabelName><SelectedValues><anyType xsi:type='{4}'>3</anyType></SelectedValues></TestResult><TestResult><LabelName>Question4Label</LabelName><SelectedValues><anyType xsi:type='{5}'>true</anyType></SelectedValues></TestResult></ArrayOfTestResult>",
"http://www.w3.org/2001/XMLSchema-instance", "http://www.w3.org/2001/XMLSchema", "xsd:int", "xsd:int", "xsd:int", "xsd:boolean")
原来这是双引号,我通过使用带单引号的字符串格式来解决它。