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.
Thanks,
Sameer
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.
Thanks,
Sameer