views:

39

answers:

1

I've been learning about DynamicObject in .NET 4.0 and was wondering if this type would be well suited to creating mock objects.

Mocking seems like a great way to use DynamicObject, but am I missing something?

  • Are there any mocking frameworks that use DynamicObject (as opposed to dynamic proxies or interception) for mocking?
  • Are there any disadvantages to using DyanmicObject for mocking (besides requiring the .NET 4.0 CLR)?
+1  A: 

The biggest drawback that I can think of is that you can call literally any method/property in the world on a dynamic types.

for example, think of the hell it would be to update your tests if your api changed - you tests would still all compile, however they would all die with runtime errors if they tried to exercise the renamed/removed methods.

This, combined with the fact that you lose all Intellisense when operating on dynamic objects leads me to believe that a dynamic-based mocking library would be more difficult to use that something proxy-based like Rhino.

Andrew Anderson
Great point. I didn't think of that :-) One of the biggest reasons I switched to Rhino Mocks from NMock a long time ago was that it supports compile-time type checking.
dariom