Castle Dynamic Proxy and moq

At SaludOnNet we are using Moq as our mocking framework for doing TDD on C#. I am very happy with Moq, it’s very simple yet powerful and flexible. It lacks the complexity of RhinoMocks (and some of it’s features) but the learning curve is very smooth, and the API almost explains itself. We love it.

We recently, started using Castle Dynamic Proxy to add interception capability to our controller-service layer boundary, we mainly use it to decorate the service methods with authorization, cache directives, etc. quite handy.

The problem

Moq also uses Castle Dynamic Proxy to implement the mock classes. Mock objects are actually instances of proxy classes using Castle.

When creating proxies of the services in the production code, our controller unit tests went nuts because the service classes where already proxied by moq beforehand. As a side effect, the unit tests started actually executing the original implementations of the mocked classes!

The solution

The best one we have found so far is to alter our production code so it checks for a moq mock instance using reflection. In that case it skips the proxy creation altogether and just adds our interceptors to the existing proxy. This has a performance penalty, but it is testable and it put us back on track right away. I will update this post and include some code snippet if we come up with something cleaner.


istepaniuk

About Iván Stepaniuk

I have been creating software for more than twenty years in a wide variety of stacks, languages and platforms. I advocate Software Craftsmanship and the Agile Manifesto, this has been a great motivation and helps me to continuously reinvent myself as a better developer that makes better quality software.

See my about page


©2014 Iván Stepaniuk. Licensed under CC-BY-SA
Site powered by Jekyll and the Noita theme, built with Foundation
RSS Feed