title: How to unit test that a Windows Workflow Foundation (WF) activity can be serialized?
body: <p>Problem: during execution, instances of classes that derives from <code>System.Workflow.ComponentModel.Activity</code> is serialized by the workflow engine. I'd like to unit test these types in order to ensure that they can be serialized. As of now, these exceptions only show up in production.</p>
  
  <p>Non-working solution:</p>
  
  <pre><code>public class UnitTestActivity : Activity
  {}
  
  [Test]
  public void ShouldBeSerializable()
  {
     var activity = new UnitTestActivity();
  
     activity.Clone(); // throws exception here
  }
  </code></pre>
  
  <p>The test above yields the following exception "System.InvalidOperationException : This is an invalid design time operation. You can only perform the operation at runtime."</p>
  
  <p>I've also tried the <code>activity.Save(...)</code> method which then throws the same exception. The code I used is:</p>
  
  <pre><code>public static void SerializeToFile( Activity activity )
  {
     using (var fileStream = new FileStream( GetFilePath(), FileMode.Create ))
     {
        IFormatter formatter = new BinaryFormatter { SurrogateSelector = ActivitySurrogateSelector.Default };
  
        activity.Save( fileStream, formatter );
     }
  }
  </code></pre>