Friday, 29 April 2016

How to verify that a class is serializable?

Recently I've came into investigating a failing End-to-End test that was failing inside the WCF service with an exception. The exception was related to WCF serialization of the return type of the service. As usually I decided to tackle this issue in the TDD way:

  1. Write a fialing test that reproduces the issue
  2. Write the minimum amount of code to make the test pass
  3. Refactor if needed

I already had a failing End to End test that was failing, but it was a heavy and complex test. All I wanted to a small, fast, isolated and reproducible test that would test only the serialization of that class. We're using FluentAssertions library for readable and intuitive assertions in our unit tests I checked whether it contains a way to verify whether a class would be successfully serialized in WCF. Unfortunately, it had two similar methods:

I need to explain what these methods exactly do for those who are not familiar with them:

  1. Serialize the instance using the selected serialization method: binary or XML
  2. Deserialize the result of the above serialization
  3. Compare the values of the resulted instance with the original one
It was exactly what I needed, but these methods use different serialization methods than WCF. WCF uses DataContract serialization. I decided to contribute to FluentAssertions library and to add the required method. Dennis Doomen generously accepted my pull request and the new method is available in the official nuget package starting from version 4.5.0. Here how it looks like with the new method:


Last but not least: if you encounter into WCF serialization issues, take a look at my post from 2010 with some WCF Serialization Tips.


Post a Comment