This is a migrated thread and some comments may be shown as answers.

Mock.SetupStatic Exception: GenericArguments violates the constraint

3 Answers 128 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Livio
Top achievements
Rank 1
Livio asked on 25 Jul 2012, 03:11 PM
Hi,

I'm experiencing the following problem.
I'm trying to mock this class:

public class UnmockableClass<T>
    {
        private bool _dummyvariable;
  
        public UnmockableClass()
        {
            _dummyvariable = true;
        }
  
        public static UnmockableClass<T> GetInstance<TDescriptor>() where TDescriptor : IDescriptor<T>
        {
           throw new NotImplementedException();
        }
    }

inside this test

[TestClass]
    public class UnmockableClassTest
    {
        [TestMethod]
        public void SimpleTest()
        {
            Mock.SetupStatic<UnmockableClass<AvailableID>>();
            //nothing else. The test will fail with just this line of code above
  
        }
    }

But the test fails and I obtain this exception:
Test 'JustMockIssueTestMock.UnmockableClassTest.SimpleTest' failed: Test method JustMockIssueTestMock.UnmockableClassTest.SimpleTest threw exception: 
System.ArgumentException: GenericArguments[0], 'JustMockIssue.IDescriptor`1[T]', on 'JustMockIssue.UnmockableClass`1[JustMockIssue.AvailableID] GetInstance[TDescriptor]()' violates the constraint of type 'TDescriptor'. ---> System.Security.VerificationException: Method JustMockIssue.UnmockableClass`1[JustMockIssue.AvailableID].GetInstance: type argument 'JustMockIssue.IDescriptor`1[T]' violates the constraint of type parameter 'TDescriptor'.
    at System.RuntimeMethodHandle.GetStubIfNeeded(RuntimeMethodHandleInternal method, RuntimeType declaringType, RuntimeType[] methodInstantiation)
    at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
     --- End of inner exception stack trace ---
    at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
    at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
    at Telerik.JustMock.Weaver.WeaverInterceptorBuilder.DefineInterceptor(MethodBase methodBase, FieldBuilder fldInterceptor, Type[] parameters)
    at Telerik.JustMock.Weaver.DynamicInjector.EmitInterceptor(ILGenerator il, MethodBase methodInfo)
    at Telerik.JustMock.Weaver.DynamicInjector.Inject(Type targetType, MethodBase methodBase, MethodInfo containerMethodInfo)
    at Telerik.JustMock.Weaver.DynamicInjector.Inject(Type targetType, MethodBase methodBase, MethodInfo injectingMethod, Boolean force)
    at Telerik.JustMock.Weaver.DynamicInjector.Inject(Type targetType, MethodBase methodBase)
    at Telerik.JustMock.MockManager.SetupMock(Behavior behavior, Boolean static)
    at Telerik.JustMock.MockManager.SetupStatic()
    at Telerik.JustMock.Mock.SetupStatic(Type targetType, Behavior behavior, StaticConstructor staticConstructor)
    at Telerik.JustMock.Mock.SetupStatic[T]()
    UnmockableClassTest.cs(17,0): at JustMockIssueTestMock.UnmockableClassTest.SimpleTest()

Do you have any idea why this happens? I wasn't able to find any help about this problem in the forum, except this one.

I'm working with JustMock Q2 2012 (trial version), VS2010 and Win7x64.
Thanks in advance
Livio

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 30 Jul 2012, 05:12 PM
Hi Livio,
Thanks again for reporting the issue. However, it is possible to write the test in the following way that fixes the problem:

var unmockable = new UnmockableClass<AvailableID>();
 
Mock.Arrange(() => UnmockableClass<AvailableID>.GetInstance<YourDescriptor>()).Returns(unmockable);

In addition, I got your exception for Mock.SetupStatic which is logged and hopefully will be fixed in the upcoming service pack. Here is the PITS task to follow:
http://www.telerik.com/support/pits.aspx#/public/justmock/12092 
 

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Livio
Top achievements
Rank 1
answered on 14 Aug 2012, 01:16 PM

Hi Ricky,

thanks for your answer and your suggestion; however, I'm not able to apply to my case the workaround you suggested.
In my scenario, this UnmockableClass is doing some weird things in the constructor; I have omitted this to make the example easy to understand, but let's say that the UnmockableClass, in the constructor, is calling an unavailable web service.

At this point, I can't write

Mock.Arrange(() => UnmockableClass<AvailableID>.GetInstance<YourDescriptor>()).Returns(unmockable);

because this instruction will call the real UnmockableClass constructor, that is something I can't do.
So, for what I know, my only available option could be to mock the creation of the object, writing something like


Mock.Create<UnmockableClass<AvailableID>>(Constructor.Mocked)

to avoid real actions to be executed.

But this instruction above doesn't work too and throws a new exception related to some "interceptor":

 

Test method JustMockIssueTestMock.UnmockableClassTest.SimpleTest threw exception: 
System.ArgumentException: GenericArguments[0], 'JustMockIssue.IProtocolDescriptor'
    on
'JustMockIssue.UnmockableClass`1[JustMockIssue.AvailableID] Intercept[TDescriptor](JustMockIssue.UnmockableClass`1[T], Boolean ByRef)'  violates the constraint of type 'TDescriptor'. ---> System.Security.VerificationException: Method UnmockableClass`1_Interceptor_55171d2dddaa4463b9b600d5130c4f3c[T].Intercept: type argument 'JustMockIssue.IProtocolDescriptor' violates the constraint of type parameter 'TDescriptor'.

Am I doing something wrong? Is there something else I can do to solve this issue?
Thanks in advance for your help.
Livio
0
Ricky
Telerik team
answered on 16 Aug 2012, 10:50 PM
Hi Livio,

Thanks again for contacting us.

We have released  the Q2 2012 SP1 a few days back that solves your specific issue. Please check it  out and let us know if you still having the problem. 

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
General Discussions
Asked by
Livio
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Livio
Top achievements
Rank 1
Share this question
or