title: Asserting JsonResult Containing Anonymous Type
body: <p>I was trying to unit test a method in one of my Controllers returning a JsonResult. To my surprise the following code didn't work:</p>
  
  <pre><code>[HttpPost]
  public JsonResult Test() {
      return Json(new {Id = 123});
  }
  </code></pre>
  
  <p>This is how I test it (also note that the test code resides in another assembly):</p>
  
  <pre><code>// Act
  dynamic jsonResult = testController.Test().Data;
  
  // Assert
  Assert.AreEqual(123, jsonResult.Id);
  </code></pre>
  
  <p>The <code>Assert</code> throws an exception:</p>
  
  <blockquote>
    <p>'object' does not contain a definition for 'Id'</p>
  </blockquote>
  
  <p>I've since resolved it by using the following:</p>
  
  <pre><code>[HttpPost]
  public JsonResult Test() {
     dynamic data = new ExpandoObject();
     data.Id = 123;
     return Json(data);
  }
  </code></pre>
  
  <p>I'm trying to understand why isn't the first one working ? It also seems to be working with basically anything BUT an anonymous type.</p>