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

How to Mock Type.GetMethod()

3 Answers 73 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nacho
Top achievements
Rank 1
Nacho asked on 07 Mar 2014, 09:00 AM
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.



3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 11 Mar 2014, 09:07 AM
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.

0
Nacho
Top achievements
Rank 1
answered on 11 Mar 2014, 10:32 AM
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 
0
Todor
Telerik team
answered on 14 Mar 2014, 07:28 AM
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.

 
Tags
General Discussions
Asked by
Nacho
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Nacho
Top achievements
Rank 1
Todor
Telerik team
Share this question
or