Problem Mocking Generic Class with Generic Methods

377 views
Skip to first unread message

Marcus Griep

unread,
Sep 6, 2007, 12:54:37 AM9/6/07
to Rhino.Mocks
I am using the C5 generics library and there are a couple of methods
within the interfaces similar to below:

interface IExtensible<T>
{
void AddAll<U>(IEnumerable<U> stuff) where U : T;
}

I implement this in a class that I am trying to test, but in mocking
it, I am getting the following exception while running the test:
"""
Message:Method 'AddAll' on type
'IExtensible`1Proxy8f0350fd5bde40b0928d2a70a8c4edf2' from assembly
'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null' tried to implicitly override a method with weaker
type parameter constraints.
Source:mscorlib
TypeName:IExtensible`1Proxy8f0350fd5bde40b0928d2a70a8c4edf2
TargetSite:System.Type TermCreateClass(Int32,
System.Reflection.Module)
HelpLink:null
StackTrace:

at System.Reflection.Emit.TypeBuilder.TermCreateClass(Int32 handle,
Module module)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
at
Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType()
at
Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateCode(Type[]
interfaces, ProxyGenerationOptions options)
at Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxy(Type
theClass, Type[] interfaces, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxyType(Type
baseClass, Type[] interfaces, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type
targetType, Type[] interfaces, ProxyGenerationOptions options,
Object[] constructorArgs, IInterceptor[] interceptors)
at Rhino.Mocks.MockRepository.MockClass(CreateMockState
mockStateFactory, Type type, Type[] extras, Object[]
argumentsForConstructor)
at Rhino.Mocks.MockRepository.CreateMockObject(Type type,
CreateMockState factory, Type[] extras, Object[]
argumentsForConstructor)
at Rhino.Mocks.MockRepository.PartialMultiMock(Type type, Type[]
extraTypes, Object[] argumentsForConstructor)
at Rhino.Mocks.MockRepository.PartialMock(Type type, Object[]
argumentsForConstructor)
at Rhino.Mocks.MockRepository.PartialMock[T](Object[]
argumentsForConstructor)
at KeyedCollections.UnitTests.KeyedCollectionBaseFixture.SetUp() in
f:\C5\KeyedCollections\KeyedCollections\UnitTests
\KeyedCollectionBaseFixture.cs:line 37
"""

Line 37 is:
mockedPairs = Mocks.DynamicMock<IExtensible<KeyValuePair<Key,
Record>>>();

Am I doing something incorrectly? Thanks for your help.

Marcus Griep

unread,
Sep 6, 2007, 12:56:36 AM9/6/07
to Rhino.Mocks
Sorry, the PartialMocks in the StackTrace are from a prior run on the
abstract class; I tried mocking the interface with dynamic and regular
mocks as well as stubs and got the same error.

Stefan Podskubka

unread,
Sep 6, 2007, 9:56:10 AM9/6/07
to Rhino...@googlegroups.com

Marcus Griep

unread,
Sep 6, 2007, 4:19:59 PM9/6/07
to Rhino.Mocks
Thanks.

On Sep 6, 9:56 am, Stefan Podskubka <s.podsku...@hcs.at> wrote:
> Marcus,
>

> I think this is the same issue that is already discussed here:http://groups.google.com/group/RhinoMocks/browse_thread/thread/69fca6...

> >> Am I doing something incorrectly? Thanks for your help.- Hide quoted text -
>
> - Show quoted text -

Mads

unread,
Sep 18, 2007, 2:13:32 PM9/18/07
to Rhino.Mocks
It looks like Microsoft is closing it as "As Design".

http://connect.microsoft.com/VisualStudio/feedback/SearchResults.aspx?SearchQuery=+tried+to+implicitly+implement+an+interface+method+with+weaker+type+parameter+constraints

> > - Show quoted text -- Skjul tekst i anførselstegn -
>
> - Vis tekst i anførselstegn -

Ayende Rahien

unread,
Sep 18, 2007, 2:48:49 PM9/18/07
to Rhino...@googlegroups.com
This pisses me off to no end.
http://www.ayende.com/Blog/archive/2007/09/18/Microsoft-Connect-Redefining-bugs-as-features-as-a-standard-operation.aspx

On 9/18/07, Mads <ites...@gmail.com> wrote:

It looks like Microsoft is closing it as "As Design".

http://connect.microsoft.com/VisualStudio/feedback/SearchResults.aspx?SearchQuery=+tried+to+implicitly+implement+an+interface+method+with+weaker+type+parameter+constraints

On 6 Sep., 22:19, Marcus Griep < neoeinst...@gmail.com> wrote:
> Thanks.
>
> On Sep 6, 9:56 am, Stefan Podskubka <s.podsku...@hcs.at> wrote:
>
>
>
> > Marcus,
>
> > I think this is the same issue that is already discussed here:http://groups.google.com/group/RhinoMocks/browse_thread/thread/69fca6. ..
>
> > Stefan
>
> > Marcus Griep wrote:
> > > Sorry, the PartialMocks in the StackTrace are from a prior run on the
> > > abstract class; I tried mocking the interface with dynamic and regular
> > > mocks as well as stubs and got the same error.
>
> > > On Sep 6, 12:54 am, Marcus Griep <neoeinst...@gmail.com> wrote:
> > >> I am using the C5 generics library and there are a couple of methods
> > >> within the interfaces similar to below:
>
> > >> interface IExtensible<T>
> > >> {
> > >>    void AddAll<U>(IEnumerable<U> stuff) where U : T;
>
> > >> }
>
> > >> I implement this in a class that I am trying to test, but in mocking
> > >> it, I am getting the following exception while running the test:
> > >> """
> > >> Message:Method 'AddAll' on type
> > >> 'IExtensible`1Proxy8f0350fd5bde40b0928d2a70a8c4edf2' from assembly
> > >> 'DynamicProxyGenAssembly2, Version= 0.0.0.0, Culture=neutral,
> > >>    at Rhino.Mocks.MockRepository.PartialMultiMock (Type type, Type[]

Fabian Schmied

unread,
Sep 18, 2007, 4:03:42 PM9/18/07
to Rhino...@googlegroups.com

No, this is just a C# compiler bug I found in passing when
investigating the problem after Ayende had reported it to the Castle
group. That's the bug I opened for the actual problem:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=290313

It seems to be fixed in Orcas and the upcoming .NET 2.0 service pack.

Also, as I've found out, "Closed By Design" doesn't necessarily mean
the bug won't be fixed, it might just be fixed in another place.
Still, Connect communication from MS to the bug reporter sucks most of
the time. It's really disappointing to see a bug closed without any
(reasonable) comment after you've spent some time writing the bug
report.

Fabian

Fabian Schmied

unread,
Sep 19, 2007, 4:41:29 AM9/19/07
to Rhino...@googlegroups.com
> No, this is just a C# compiler bug I found in passing when
> investigating the problem after Ayende had reported it to the Castle
> group.

To clafirfy, it's either a compiler bug or a verifier bug. However,
it's not really the same as the RhinoMocks problem.

The original bug is in DynamicProxy and occurrs when overriding or
implementing generic methods with type constraints that refer to the
type parameters of the declaring class. DynamicProxy uses the wrong
type parameters, leading to the "weak type parameter constraints"
exception.

Ayende created a patch fixing this, but this led to another exception:

System.Security.VerificationException : Method
Castle.DynamicProxy.Tests.GenericTestInterface`1
[System.Collections.ObjectModel.Collection`1[System.String]].GenericTestMethod:
type argument 'T2' violates the constraint of type parameter 'T2'.

And that's a CLR bug related to the ldtoken IL instruction. I reported
that one here:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=290313

And it will be fixed in the upcoming service pack (together with a few
other issues we found with DynamicProxy).

See also the Castle group:
http://groups.google.com/group/castle-project-devel/browse_thread/thread/1697e02d96c9c2df/8f1969c2267e35d8

Fabian

Ayende Rahien

unread,
Sep 19, 2007, 5:19:48 AM9/19/07
to Rhino...@googlegroups.com
That makes me feel much better, thanks for clarifying it.

Fabian Schmied

unread,
Sep 21, 2007, 5:47:41 PM9/21/07
to Rhino...@googlegroups.com
> > http://connect.microsoft.com/VisualStudio/feedback/SearchResults.aspx?SearchQuery=+tried+to+implicitly+implement+an+interface+method+with+weaker+type+parameter+constraints
>
> No, this is just a C# compiler bug I found in passing when
> investigating the problem after Ayende had reported it to the Castle
> group.

Just for the record, the compiler or verifier bug has actually been
reexamined and reopened. While it's not a RhinoMocks-related bug, it's
still nice to see it get some attention after all. Maybe this was
because a few people here seem to have voted on it, or maybe someone's
read Ayende's blog :)

Fabian

Reply all
Reply to author
Forward
0 new messages