moq/labs

Add support for being able to proxy existing class instance

tapika opened this issue · 3 comments

By default when you create new Mock, you cannot use existing class instance - meanwhile in Moq v 4 - I've noticed that Castle.DynamicProxy.ProxyGenerator supports providing existing class instance - you can use either CreateInterfaceProxyWithTarget or CreateClassProxyWithTarget - and proxy existing class instance.

I do understand that Moq v5 is slightly different than Moq v4 - but I think its possible to use same concept as well.

So maybe there should be alternative constructor:

class Mock<T>...

      Mock(T t);  // Use by default all methods from t unless overridden by Setup function call.

Which would allow to pass class instance to be proxied.

This feature would be useful in case if want to use majority of same functionality as T defines, by maybe override by couple of methods via Setup.

stakx commented

This feature would be useful in case if want to use majority of same functionality as T defines, by maybe override by couple of methods via Setup.

That's basically what CallBase does already, right?

I've harvested all Moq tests forth and back, and of course checked through CallBase - and now also double checked.
Can it be used to pass instance to mock ?
Like proposed above T t would be passed to mock, and mock would use it.

Intrested particularly in instance.

stakx commented

No, but you wouldn't need to delegate to some object instance in the first place, if all you want is to reuse some or all of the existing functionality from the mocked (base) class. That's what CallBase gives you.