How to Mock Type.GetMethod()

4 posts, 0 answers
  1. Nacho
    Nacho avatar
    15 posts
    Member since:
    Oct 2013

    Posted 07 Mar 2014 Link to this post

    Hello,

    we need to mock Type.GetMethod() but seems that is not directly possible as when we are doing:

    Mock.Arrange(() => fakeType.GetMethod("Validate")).Returns(fakeDelegate);

    we are getting the following exception:

    Telerik.JustMock.MockException: Cannot mock non-inheritable member 'System.Reflection.MethodInfo GetMethod(System.String)' on type 'System.Type' due to CLR limitations.

    Anyway I a support ticket one of your team members told me something that can be useful (the question was related with another thing). He said:

    " ... for these classes [talking about MethodIndo] we support only non-elevated mocking, through Mock.Create<T>() ..."

    Not sure if this can help in our current problem or not. Is there any way to mock what we need?

    Thanks in advance.



  2. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 11 Mar 2014 Link to this post

    Hello Ignacio,

    Mocking non-inheritable members of System.Type is currently forbidden, because large parts of .NET itself depends on System.Type to work as is. In your case, if mocking GetMethod was enabled, it could result in a hard crash of the CLR.

    If you really need to mock System.Type like that, we could explore possible solutions, but I urge you to refactor your code in a way that doesn't require it, instead.

    Regards,
    Stefan
    Telerik

    DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

  3. DevCraft R3 2016 release webinar banner
  4. Nacho
    Nacho avatar
    15 posts
    Member since:
    Oct 2013

    Posted 11 Mar 2014 in reply to Stefan Link to this post

    Hello Stefan,

    we are currently loading plugin assemblies developed by others so the code that does this heavily relies on the Reflection namespace. Would be a way to try to mock it and if it crashes then just continue without mocking it?

    I thought that somehow that was the functionality of the AllowedMockableTypes class.

    Regards.

    Ignacio Soler Garcia 
    Software Engineer 

    Omron Europe B.V. 
    MSC - Mechatronics Software Center 
    Josep Plà, 2 Edifici B2, Planta 10 | 08019 Barcelona - Spain 
    Tel. +34 93 214 0615, Fax. +34 93 214 0631 
    Email: ignacio.soler@eu.omron.com 
  5. Todor
    Admin
    Todor avatar
    63 posts

    Posted 14 Mar 2014 Link to this post

    Hi Ignacio,

    As my colleague Stefan said, we have very hard technical obstacles to mock Type members.

    I can think of two possible solutions. The easier one is to refactor your code base to abstract away usage of Reflection API. Test this code with integration tests and the other parts of the system with unit tests and mocked objects.

    A more involved solution would be to introduce already available frameworks to mange extensions. One of the most used ones, which we use internally in Telerik with great success, is MEF. It works for .net 3.5 and up and is well tested and supported by Microsoft.

    I hope this information helps you to resolve your issue.

    Regards,
    Todor
    Telerik
     

    DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

     
Back to Top