How to mock a dynamic object
Posted
by Daniel Cazzulino
on ASP.net Weblogs
See other posts from ASP.net Weblogs
or by Daniel Cazzulino
Published on Wed, 29 Jun 2011 12:03:15 GMT
Indexed on
2011/06/29
16:23 UTC
Read the original article
Hit count: 360
.NET
Someone asked me how to mock a dynamic object with Moq, which might be non-obvious.
Given the following interface definition:
public interface IProject { string Name { get; } dynamic Data { get; } }
When you try to setup the mock for the dynamic property values, you get:
What’s important to realize is that a dynamic object is just a plain object, whose properties happen to be resolved at runtime. Kinda like reflection, if you will: all public properties of whatever object happens to be the instance, will be resolved just fine at runtime.
Therefore, one way to mock this dynamic is to just create an anonymous type with the properties we want, and set the dynamic property to return that:...
© ASP.net Weblogs or respective owner