The given key was not present in the dictionary.

3 posts, 0 answers
  1. A
    A avatar
    1 posts
    Member since:
    Sep 2014

    Posted 11 Feb 2015 Link to this post

    I have been testing JustMock Lite as a replacement to RhinoMocks and converting some of our existing unit tests to learn the tool and asses if this is better.

    I am using VB.NET, VS 2013 and .NET 4.

    I came across this error after converting one particular unit test:

    Result Message: System.Collections.Generic.KeyNotFoundException : The given key was not present in the dictionary.
    Result StackTrace:
    at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
    at Telerik.JustMock.Core.Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.GetGenericArgumentsFor(MethodInfo genericMethod)
    at Telerik.JustMock.Core.Castle.DynamicProxy.Generators.InvocationTypeGenerator.GetCallbackMethod(AbstractTypeEmitter invocation)
    at Telerik.JustMock.Core.Castle.DynamicProxy.Generators.InvocationTypeGenerator.ImplementInvokeMethodOnTarget(AbstractTypeEmitter invocation, ParameterInfo[] parameters, MethodEmitter invokeMethodOnTarget, Reference targetField)
    at Telerik.JustMock.Core.Castle.DynamicProxy.Generators.InvocationTypeGenerator.ImplemementInvokeMethodOnTarget(AbstractTypeEmitter invocation, ParameterInfo[] parameters, FieldReference targetField, MethodInfo callbackMethod)
    at Telerik.JustMock.Core.Castle.DynamicProxy.Generators.InvocationTypeGenerator.Generate(ClassEmitter class, ProxyGenerationOptions options, INamingScope namingScope)
    at Telerik.JustMock.Core.Castle.DynamicProxy.Contributors.ClassProxyTargetContributor.BuildInvocationType(MetaMethod method, ClassEmitter class, ProxyGenerationOptions options)
    at Telerik.JustMock.Core.Castle.DynamicProxy.Contributors.ClassProxyTargetContributor.GetMethodGenerator(MetaMethod method, ClassEmitter class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod)
    at Telerik.JustMock.Core.Castle.DynamicProxy.Contributors.CompositeTypeContributor.ImplementMethod(MetaMethod method, ClassEmitter class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod)
    at Telerik.JustMock.Core.Castle.DynamicProxy.Contributors.CompositeTypeContributor.Generate(ClassEmitter class, ProxyGenerationOptions options)
    at Telerik.JustMock.Core.Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateType(String name, Type[] interfaces, INamingScope namingScope)
    at Telerik.JustMock.Core.Castle.DynamicProxy.Generators.ClassProxyGenerator.<>c__DisplayClass1.<GenerateCode>b__0(String n, INamingScope s)
    at Telerik.JustMock.Core.Castle.DynamicProxy.Generators.BaseProxyGenerator.ObtainProxyType(CacheKey cacheKey, Func`3 factory)
    at Telerik.JustMock.Core.Castle.DynamicProxy.Generators.ClassProxyGenerator.GenerateCode(Type[] interfaces, ProxyGenerationOptions options)
    at Telerik.JustMock.Core.Castle.DynamicProxy.DefaultProxyBuilder.CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options)
    at Telerik.JustMock.Core.Castle.DynamicProxy.ProxyGenerator.CreateClassProxy(Type classToProxy, Type[] additionalInterfacesToProxy, ProxyGenerationOptions options, Object[] constructorArguments, IInterceptor[] interceptors)
    at Telerik.JustMock.Core.MocksRepository.Create(Type type, MockCreationSettings settings)
    at Telerik.JustMock.MockBuilder.Create(MocksRepository repository, Type type, Object[] constructorArgs, Nullable`1 behavior, Type[] additionalMockedInterfaces, Nullable`1 mockConstructorCall, IEnumerable`1 additionalProxyTypeAttributes, List`1 supplementaryBehaviors, List`1 fallbackBehaviors, List`1 mixins, Expression`1 interceptorFilter)
    at Telerik.JustMock.Mock.<>c__DisplayClass78`1.<Create>b__77()
    at Telerik.JustMock.Core.ProfilerInterceptor.GuardInternal[T](Func`1 guardedAction)
    at Telerik.JustMock.Mock.Create[T](Behavior behavior)
    at ConsoleApplication1.TestClass1.Test() in Temporary Projects\ConsoleApplication1\Class1.vb:line 27

    After some trial and error I found out what the issue is and the following code will reproduce the error. The issue is that the implementation of an interface function that has generics uses a different variable name for the types. In the example below the interface method has TParent and TChild but when implemented the generic types are called ParentT and ChildT.

    Imports NUnit.Framework
    Imports Telerik.JustMock

    Public Class Class1
        Implements myInterface

        Public Function myMethod(Of ParentT, ChildT)(a As ParentT) As ChildT Implements myInterface.myMethod
            Return Nothing
        End Function
    End Class

    Public Interface myInterface
        Function myMethod(Of TParent, TChild)(a As TParent) As TChild
    End Interface

    <TestFixture> _
    Public Class TestClass1
        <Test> Public Sub Test()
            Dim c As Class1
            c = Mock.Create(Of Class1)(Behavior.RecursiveLoose)
        End Sub
    End Class

    I assume this is a bug or do we need to be more careful in our coding? RhinoMocks is able to handle this okay.

  2. Kaloyan
    Kaloyan avatar
    878 posts

    Posted 16 Feb 2015 Link to this post

    Hello Sameer,

    Thank you for contacting us. I managed to reproduce the issue on my side. Unfortunately, we will require a bit more time in order to investigate what is causing it. We will get back to you as soon as we have results.

    Thank you for the understanding and the patience in advance.


    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

  3. Stefan
    Stefan avatar
    198 posts

    Posted 17 Feb 2015 Link to this post

    Hello Sameer,

    There is indeed a bug in Castle's DynamicProxy library that causes this issue. As a token of gratitude for reporting the bug together with a straightforward repro, I've granted you some Telerik points.

    The bug is now fixed and the bug fix will be made available in the Q1 release due next week.

    If there's anything else that I can help you with during your trial, don't hesitate to ping me.


    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

Back to Top