[英]Error in creating xml from an list of mock objects
I wanted to generate a sample xml so I write a unit test in which I have created an object using moq. 我想生成一个示例xml,因此我编写了一个单元测试,其中已使用moq创建了一个对象。 I tried to serialize it like this:
我试图像这样序列化它:
private AssetDescription GetAssetDescription(string description, string type, string name, string iconUrl)
{
var asstDesp = new Mock<AssetDescription>(type);
asstDesp.Setup(m => m.Description).Returns(description);
asstDesp.Setup(m => m.Type).Returns(type);
asstDesp.Setup(m => m.Name).Returns(name);
asstDesp.Setup(m => m.IconUrl).Returns(iconUrl);
return asstDesp.Object;
}
Note: here AssetDescription
is a class like this: 注意:此处
AssetDescription
是类似这样的类:
[DataContract]
public class AssetDescription
{
[DataMember]
public virtual string Type { get; set; }
[DataMember]
public virtual string Name { get; set; }
[DataMember]
public virtual string Description { get; set; }
[DataMember]
public virtual string IconUrl { get; set; }
public AssetDescription(string type)
{
Type = type;
}
public AssetDescription()
{
// I have added a parameter less constructor to xml serialization.
}
}
XML serialization method: XML序列化方法:
public string SerializeObject(object obj)
{
var xmlDoc = new XmlDocument();
var serializer = new XmlSerializer(obj.GetType());
using (var ms = new MemoryStream())
{
serializer.Serialize(ms, obj);
ms.Position = 0;
xmlDoc.Load(ms);
return xmlDoc.InnerXml;
}
}
Now I can serialize the AssetDescription
successfully like this : 现在,我可以像这样成功地序列化
AssetDescription
:
var ds = GetAssetDescription("Description1", "type1", "name1", "iconurl1");
var dsxml = SerializeObject(ds);
Problem : AssetDescription
is a part of a list and that list is part of some other object I have created that object using moq. 问题 :
AssetDescription
是列表的一部分,而该列表是我使用moq创建该对象的其他对象的一部分。 I have break down to this after some testing:- I am not able to serialize a list of AssetDescription
it is throwing error. 经过一些测试,我已经分解为以下内容:-我无法序列化
AssetDescription
错误的AssetDescription
列表 。
Here is my list creating method: 这是我的列表创建方法:
private List<AssetDescription> GetListAssetDescriptions()
{
var lst = new List<AssetDescription>
{
GetAssetDescription("Description1", "type1", "name1", "iconurl1"),
GetAssetDescription("Description2", "type2", "name2", "iconurl2"),
GetAssetDescription("Description3", "type3", "name3", "iconurl3"),
GetAssetDescription("Description4", "type4", "name4", "iconurl4")
};
return lst;
}
I tried to serialize it like this: 我试图像这样序列化它:
var fgh = GetListAssetDescriptions();
var fghd = SerializeObject(fgh);
but this error occurs: 但是会发生此错误:
There was an error generating the XML document
生成XML文档时出错
Questions: 问题:
can I generate xml from mock objects? 我可以从模拟对象生成xml吗?
if yes, then does anybody know how to solve this error? 如果是,那么有人知道如何解决此错误吗?
Not an answer, but I'm puzzled with this question. 没有答案,但是我对这个问题感到困惑。 Let me ask you a few questions:
让我问你几个问题:
AssetDescription
is a POCO, not an interface. AssetDescription
是POCO,而不是接口。 As far as your code goes, this class does not do anything. Mocks are for testing only. 模拟仅用于测试。 One must think really hard before adding a
Moq
reference to non-testing project. 在将
Moq
引用添加到非测试项目之前,必须认真思考。 Mocks have quite complex internal structure - they are designed to pretend to be something they are not. cks子具有相当复杂的内部结构-它们被设计成假装不是。 XML serialisation was not part of design for these guys, so no surprise you can't serialise mock into XML.
对于这些人来说,XML序列化不是设计的一部分,因此,您不能将模拟序列化为XML不足为奇。 And I'll go that far and say that there is no work-around for this.
我会走得更远,说这没有解决方法。 Because mocks must not be serialised.
因为模拟不能被序列化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.