Mocking concrete types without calling the constructor

617 views
Skip to first unread message

mcintyre321

unread,
Mar 9, 2010, 8:48:32 AM3/9/10
to Moq Discussions
I was just reading http://garmhold.blogspot.com/2010/03/relax-you-can-mostly-stop-using.html
- it seems in Java you can create a mock without calling the base
class constructor - sounds good to me! Would it be possible to do this
in .NET using FormatterServices.Get(Safe)UninitializedObject method
(see http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatterservices_members.aspx).
This might also alleviate the requirement to add empty protected
constructors to types that need to be mocked, right?

Daniel Cazzulino

unread,
Mar 11, 2010, 1:39:27 PM3/11/10
to moq...@googlegroups.com
mmm... interesting. not sure DynamicProxy allows this... Krzysztof?


/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471


Krzysztof Koźmic (2)

unread,
Mar 12, 2010, 3:41:12 AM3/12/10
to Moq Discussions
I'm not sure for inheritance-based proxies CLR lets you go away
without calling base .ctor
For composition based proxies I think it is technically possible,
although DP does not support it, because no one ever requested it.
I can take a look at it over the weekend.

On 11 Mar, 19:39, Daniel Cazzulino <k...@clariusconsulting.net> wrote:
> mmm... interesting. not sure DynamicProxy allows this... Krzysztof?
>
> /kzu
>
> --
> Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1
> 425.329.3471
>

> On Tue, Mar 9, 2010 at 10:48, mcintyre321 <mcintyre...@gmail.com> wrote:
> > I was just reading

> >http://garmhold.blogspot.com/2010/03/relax-you-can-mostly-stop-using....


> > - it seems in Java you can create a mock without calling the base
> > class constructor - sounds good to me! Would it be possible to do this
> > in .NET using FormatterServices.Get(Safe)UninitializedObject method
> > (see

> >http://msdn.microsoft.com/en-us/library/system.runtime.serialization....

Krzysztof Koźmic (2)

unread,
Mar 12, 2010, 4:19:44 AM3/12/10
to Moq Discussions
[IL]: Error: [D:\test\CastleDynProxy2.dll :
Castle.Proxies.IServiceProxy::.ctor][mdToken=0x600000f][offset
0x0000000E] Return from .ctor when this is uninitialized.


looks like it is not possible in .NET

mcintyre321

unread,
Mar 12, 2010, 4:30:49 AM3/12/10
to Moq Discussions
Dang. Thanks for taking a look though.

On Mar 12, 9:19 am, Krzysztof Koźmic (2) <krzysz...@kozmic.pl> wrote:
> [IL]: Error: [D:\test\CastleDynProxy2.dll :
> Castle.Proxies.IServiceProxy::.ctor][mdToken=0x600000f][offset
> 0x0000000E] Return from .ctor when this is uninitialized.
>
> looks like it is not possible in .NET
>

Krzysztof Koźmic (2)

unread,
Mar 12, 2010, 8:35:25 AM3/12/10
to Moq Discussions
More precisely - this code would work (in full trust). The assembly is
unverifiable though and in medium trust would throw an exception.

However Moq could theoretically mock objects having no default .ctor
without having to do any changes in DP itself.

isntead of using proxyGenerator.CreateClassProxy, which rerurns proxy
instance, it could call proxyFactory.CreateClassProxyType which
returns type. It then could call
FormatterServices.Get(Safe)UninitializedObject, set all the fields via
Reflection and potentially be good to go.

This is a hack though, and I'm not sure it's such a good idea anyway.
TTBOMK Moq (same as Rhino.Mocks) allows you to call non-default .ctors
on your mocked class, so it's a pretty artificial issue anyway.

On 12 Mar, 10:30, mcintyre321 <mcintyre...@gmail.com> wrote:
> Dang. Thanks for taking a look though.
>

Reply all
Reply to author
Forward
0 new messages