我有一个方法WriteXml。
public void WriteXml(XmlWriter writer)
{
if (this.value == null)
{
writer.WriteValue(this.value?.ToString() ?? string.Empty);
return;
}
var type = this.value.GetType();
var memInfo = type.GetMember(this.value.ToString());
if (memInfo.Length > 0)
{
var attrs = memInfo[0].GetCustomAttributes<XmlEnumAttribute>();
if (attrs != null && attrs.IsNotEmpty())
{
writer.WriteValue(attrs);
return;
}
}
writer.WriteValue(this.value.ToString());
}
当我使用单元测试进行测试时。即,
[Theory]
[InlineData(null, "")]
[InlineData(exposureSValue.NA, "N/A")]
[InlineData(exposureSValue.Last15Days, "Last 15 Days")]
public void WriteXmlTest([CanBeNull] exposureSValue? policyCustomEnumValue, [CanBeNull] string writtenValue)
{
// Arrange
var mockXmlWriter = this.Mock.Create<XmlWriter>(Moq.MockBehavior.Loose);
mockXmlWriter.Setup(x => x.WriteValue(writtenValue));
var policyCustomerEnum = new PolicyCustomEnum<exposureSValue>(policyCustomEnumValue);
// Act and Assert
policyCustomerEnum.WriteXml(mockXmlWriter.Object);
}
当我运行此单元测试时,我收到错误消息
The following setups were not matched:
XmlWriter x=>x.WriteValue("N/A");
请帮我解决这个问题。我也尝试将测试方法中的参数更改为exposureSValue类型,但它仍然是相同的。
“过去15天”我也收到错误。
如何摆脱该错误消息?
答案 0 :(得分:0)
实际问题在于writer.WriteValue(attrs);
它没有在attrs
中写入值。
我用这种方式编写,测试用例中没有错误。
public void WriteXml(XmlWriter writer)
{
if (this.value == null)
{
writer.WriteValue(this.value?.ToString() ?? string.Empty);
return;
}
var type = this.value.GetType();
var memberInfo = type.GetMember(this.value.ToString());
if (memberInfo.Length > 0)
{
var attribute = memberInfo[0].GetCustomAttributes<XmlEnumAttribute>();
if (attribute != null && attribute.IsNotEmpty())
{
var xmlEnumAttribute = attribute.FirstOrDefault();
if (xmlEnumAttribute != null)
{
writer.WriteValue(xmlEnumAttribute.Name);
return;
}
}
}
writer.WriteValue(this.value.ToString());
}
...谢谢!