Mock.SetupStatic Exception: GenericArguments violates the constraint

4 posts, 0 answers
  1. Livio
    Livio avatar
    2 posts
    Member since:
    Jul 2012

    Posted 25 Jul 2012 Link to this post

    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
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 30 Jul 2012 Link to this post

    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.

  3. DevCraft R3 2016 release webinar banner
  4. Livio
    Livio avatar
    2 posts
    Member since:
    Jul 2012

    Posted 14 Aug 2012 Link to this post

    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
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 16 Aug 2012 Link to this post

    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.

Back to Top