"dynamic" keyword and JSON data

Posted by Peter Perhác on Stack Overflow See other posts from Stack Overflow or by Peter Perhác
Published on 2011-01-06T11:22:15Z Indexed on 2011/01/06 11:53 UTC
Read the original article Hit count: 292

Filed under:
|
|
|
|

An action method in my ASP.NET MVC2 application returns a JsonResult object and in my unit test I would like to check that the returned JSON object indeed contains the expected values.

I tried this:

1. dynamic json = ((JsonResult)myActionResult).Data;
2. Assert.AreEqual(JsonMessagesHelper.ErrorLevel.ERROR.ToString(), json.ErrorLevel);

But I get a RuntimeBinderException "'object' does not contain a definition for 'ErrorLevel'".

However, when I place a breakpoint on line 2 and inspect the json dynamic variable (see picture below), it obviously does contain the ErrorLevel string and it has the expected value, so if the runtime binder wasn't playing funny the test would pass.

Snapshot of the Locals debugger window

What am I not getting? What am I doing wrong and how can I fix this? How can I make the assertion pass?

© Stack Overflow or respective owner

Related posts about c#

Related posts about unit-testing