Problem Inheriting from Mock<T>

36 views
Skip to first unread message

José F. Romaniello

unread,
Oct 8, 2009, 8:31:49 AM10/8/09
to Moq Discussions
I have a problem when ever I try to inherit from Mock, as follows:

public class DaoFactoryMock : Mock<IDaoFactory>
{
public static DaoFactoryMock New()
{
return new DaoFactoryMock();
}

public DaoFactoryMock PushDao<T>(IDao<T> dao)
{
Setup(df => df.GetDaoOf<T>()).Returns(dao);
return this;
}
}

test case:
[Test]
public void should_work()
{
var dao = new Mock<IDao<CuentaContable>>();
var factory = DaoFactoryMocker.New().PushDao(dao.Object);

var daocuenta = factory.Object.GetDaoOf<Customer>();

}

this is a trivial case and I of course will never use in this maner..
*If I run the test without debuggin it works greate*

Whenever I try to debug a piece of code with this, I run into the
following exception:


System.BadImageFormatException: Se ha intentado cargar un programa con
un formato incorrecto. (Excepción de HRESULT: 0x8007000B)
en System.Reflection.Emit.TypeBuilder._TermCreateClass(Int32 handle,
Module module)
en System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
en System.Reflection.Emit.TypeBuilder.CreateType()
en
Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType
()
en
Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType
()
en
Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCode
(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions
options)
en
Castle.DynamicProxy.DefaultProxyBuilder.CreateInterfaceProxyTypeWithoutTarget
(Type interfaceToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options)
en
Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyTypeWithoutTarget
(Type interfaceToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options)
en Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget
(Type interfaceToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options, IInterceptor[] interceptors)
en Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget
(Type interfaceToProxy, Type[] additionalInterfacesToProxy,
IInterceptor[] interceptors)
en Moq.Mock`1.<InitializeInstance>b__0()
en Moq.PexProtector.Invoke(Action action)
en Moq.Mock`1.InitializeInstance()
en Moq.Mock`1.get_Object()
en My Donno what project- test case-etc...

The translation of "Se ha intentado cargar un programa con un formato
incorrecto." will be:
"An attempt was made to load a program with an incorrect format.." or
something like that.

Will be great if you can solve... if not, this class should be sealed.

José F. Romaniello

unread,
Oct 8, 2009, 9:26:07 AM10/8/09
to Moq Discussions
I have reproduced and isolated the problem in a single file and
complete test:

http://pastie.org/646842

Pay attention to the line 7. If I remove that line, I can debug
properly.

José F. Romaniello

unread,
Oct 8, 2009, 9:49:56 AM10/8/09
to Moq Discussions
Next step, I isolate the problem deep enought to DynamicProxy:

http://pastie.org/646873

Daniel Cazzulino

unread,
Oct 8, 2009, 10:02:43 AM10/8/09
to moq...@googlegroups.com
great spelunking!
 
did you report to DP?

/kzu

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


José F. Romaniello

unread,
Oct 8, 2009, 10:34:09 AM10/8/09
to Moq Discussions
I've tested with the trunk version and is failing too.
So, I've opened a new issue:
http://support.castleproject.org/projects/DYNPROXY/issues/view/DYNPROXY-ISSUE-115

@kkozmic will have a look to that.

On 8 oct, 11:02, Daniel Cazzulino <k...@clariusconsulting.net> wrote:
> great spelunking!
>
> did you report to DP?
>
> /kzu
>
> --
> Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1
> 425.329.3471
>

José F. Romaniello

unread,
Oct 8, 2009, 10:40:31 AM10/8/09
to Moq Discussions
I mean; "also fails"
"fallando" -> failing... Bad translation. But, surely you understood
me.

On 8 oct, 11:34, José F. Romaniello <jfromanie...@gmail.com> wrote:
> I've tested with the trunk version and is failing too.
> So, I've opened a new issue:http://support.castleproject.org/projects/DYNPROXY/issues/view/DYNPRO...

Daniel Cazzulino

unread,
Oct 8, 2009, 10:49:27 AM10/8/09
to moq...@googlegroups.com
hehe, of course chavon :)
 
thanks for tracking this down!


/kzu

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


Krzysztof Koźmic (2)

unread,
Oct 9, 2009, 5:22:53 AM10/9/09
to Moq Discussions
I verified this, and it indeed _appears_ to be a very nasty bug in DP.
It does work without debugger, and it does pass PEVerify, so it's not
invalid in CLR sense, however C# debugger chokes on it.

I'm not yet able to point to the exact spot that is the cause for this
issue, but trying to do trivial recreation of the issue using bare
System.Reflection.Emit produced assembly that does work with C#
debugger.

If you want to track progress on this issue, it's here:
http://support.castleproject.org/projects/DYNPROXY/issues/view/DYNPROXY-ISSUE-115

I can only say that along the way I found bugs in Reflector and
ReSharper - just to give you an impression of now convoluted scenario
this is.

Krzysztof

On 8 Paź, 16:49, Daniel Cazzulino <k...@clariusconsulting.net> wrote:
> hehe, of course chavon :)
>
> thanks for tracking this down!
>
> /kzu
>
> --
> Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1
> 425.329.3471
>

Chris Missal

unread,
Oct 9, 2009, 8:04:59 AM10/9/09
to moq...@googlegroups.com
Good job José, you broke .NET! ;)


2009/10/9 Krzysztof Koźmic (2) <krzy...@kozmic.pl>

José F. Romaniello

unread,
Oct 9, 2009, 9:00:14 AM10/9/09
to Moq Discussions
> I can only say that along the way I found bugs in Reflector and
> ReSharper - just to give you an impression of now convoluted scenario
> this is.

Holy moly...

Krzysztof Koźmic (2)

unread,
Oct 12, 2009, 7:54:38 AM10/12/09
to Moq Discussions
If anyone is interested in the conclusions we've arrived at, head to
http://groups.google.com/group/castle-project-devel/browse_thread/thread/3aef48223f44755b

Short story,

- This is a bug in the debugger,
- I'll post an issue to Connect about it, but I don't think it's gonna
be fixed for .NET 4.0 knowing Microsoft's workflow
- As there's no easy workaround for this, DP will for now try to
detect this scenario and give some descriptive error message about it.
- There seems to be a way to workaround it by changing how DP works
internally, but as this is a low-impact bug it's going to be part of
DP 3.0 (unless someone provides a patch).

Krzyszof

PS,

Thanks José for your help on this :)

José F. Romaniello

unread,
Oct 12, 2009, 8:57:16 AM10/12/09
to Moq Discussions
Thank you, for your work! I've been following your progress just for
learn and I think you do an amazing job!

On 12 oct, 08:54, Krzysztof Koźmic (2) <krzysz...@kozmic.pl> wrote:
> If anyone is interested in the conclusions we've arrived at, head tohttp://groups.google.com/group/castle-project-devel/browse_thread/thr...

Daniel Cazzulino

unread,
Oct 12, 2009, 4:45:46 PM10/12/09
to moq...@googlegroups.com
Ditto!

Thanks for keeping us in the loop.
--
Sent from my mobile device

Krzysztof Koźmic (2)

unread,
Oct 13, 2009, 4:42:56 AM10/13/09
to Moq Discussions
The debugger bug is fixed in VS2010.

This is a good news since the workaround I had in mind wouldn't work
in the old (current) debugger.
For now I added detecting this error and showing descriptive message
to the user, to DP trunk if you want to upgrade.

On 12 Paź, 22:45, Daniel Cazzulino <k...@clariusconsulting.net> wrote:
> Ditto!
>
> Thanks for keeping us in the loop.
>
> On 12/10/2009, Krzysztof Koźmic (2) <krzysz...@kozmic.pl> wrote:
>
>
>
>
>
> > If anyone is interested in the conclusions we've arrived at, head to
> >http://groups.google.com/group/castle-project-devel/browse_thread/thr...
Reply all
Reply to author
Forward
0 new messages