如何对我的对象是否真的可序列化进行单元测试?
我正在使用 C# 2.0 和 Nunit Test。 我有一些需要序列化的对象。 这些对象相当复杂(不同级别的继承并且包含很多对象、事件和委托)。
如何创建单元测试以确保我的对象可以安全地序列化?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
序列化对象(到内存或磁盘),反序列化它,使用反射来比较两者,然后再次运行该对象的所有单元测试(当然序列化除外),
这假设您的单元测试可以接受一个物体作为目标而不是制作自己的目标
serialize the object (to memory or disk), deserialize it, use reflection to compare the two, then run all of the unit tests for that object again (except serialization of course)
this assumes that your unit tests can accept an object as a target instead of making their own
下面是一个解决方案,递归地使用 IsSerialized 来检查对象及其所有属性是否可序列化。
Here is a solution that recursively uses IsSerializable to check that the object and all its properties are Serializable.
不幸的是,您无法真正对此进行测试。 想象一下这个案例:
Unfortunately, you can't really test for this. Imagine this case:
这是一个通用的方法:
Here is a generic way:
我在工作中的一些单元测试中有这个:
可能会帮助你......也许其他方法可能更好,但这个效果很好。
I have this in some unit test here at job:
Might help you... maybe other method can be better but this one works well.
除了上面的测试(确保序列化器接受您的对象)之外,您还需要进行往返测试。 将结果反序列化回新对象并确保两个实例是等效的。
In addition to the test above - which makes sure the serializer will accept your object, you need to do a round-trip test. Deserialize the results back to a new object and make sure the two instances are equivalent.
可能有点晚了,但是如果您使用 FluentAssertions 库,那么它有自定义断言用于 XML 序列化、二进制序列化和数据协定序列化。
Probably a bit late in the day, but if you are using the FluentAssertions library, then it has custom assertions for XML serialization, binary serialization, and data contract serialization.