Tuesday, 22 May 2012

RuntimeBinderException cannot resolver property in Unit Test in case of dynamic return.. Solved


This is an issue that i have face while doing tests for a dynamic return method, while it worked on the run, it did not work on the testing environment so it had to be a mocking return issue, i was returning a collection of anonymous types and it was, how ever  i have found the solution, to illustrate the issue here is what happened in samples.
A sample test would normally fail cause of exception runtime binder exception

 so the test simply check for a return of a function that returns a dynamic object and checks for its return, once run a run time binder exception  will be thrown in the Assert statement as the props cannot be resolved.
The issue here is with anonymous types if you use ExpandoObject instead it should work fine with no problem. So replace the anonymous type with an expand object, check below

No comments:

Post a Comment