如何进行单元测试,如果我的目标是真正的序列化?我的、单元测试、目标、序列化

2023-09-02 12:02:39 作者:晚鲸

我使用C#2.0 NUnit测试。我有一些对象,需要序列。这些对象是相当复杂的(继承不同层次,含有大量的对象,事件和委托)。

I am using C# 2.0 with Nunit Test. I have some object that needs to be serialized. These objects are quite complex (inheritance at different levels and contains a lot of objects, events and delegates).

如何创建单元测试,以确保我的目标是安全的序列化?

How can I create a Unit Test to be sure that my object is safely serializable?

推荐答案

我这个在这里的一些单元测试的工作:

I have this in some unit test here at job:

MyComplexObject dto = new MyComplexObject();
MemoryStream mem = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
try
{
    b.Serialize(mem, dto);
}
catch (Exception ex)
{
    Assert.Fail(ex.Message);
}

可以帮助你......也许其他的方法可以更好,但这个效果很好。

Might help you... maybe other method can be better but this one works well.