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

Mocking Assembly.ReflectionOnlyLoadFrom

3 Answers 95 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nick
Top achievements
Rank 1
Nick asked on 08 Apr 2011, 03:16 AM
I am trying to write a unit test for a class that internally calls the static function Assembly.ReflectionOnlyLoadFrom(string). I would like to Arrange that call to return a Mock Assembly. 

Is this even possible?

Below are my two approaches thus far, both ended with my arrangement being bypassed and the internal function being executed:

Mock.SetupStatic<Assembly>();
Mock.Arrange(() => Assembly.ReflectionOnlyLoadFrom(Arg.AnyString)).Returns(Mock.Create<Assembly>());
 
var test = Assembly.ReflectionOnlyLoadFrom("Some Assembly"); //this will throw an exception instead of returning the mock Assembly

Mock.Partial<Assembly>().For<string>((s) => Assembly.ReflectionOnlyLoadFrom(s));
Mock.Arrange(() => Assembly.ReflectionOnlyLoadFrom(Arg.AnyString)).Returns(Mock.Create<Assembly>());
 
var test = Assembly.ReflectionOnlyLoadFrom("Some Assembly"); //this will throw an exception instead of returning the mock Assembly

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 12 Apr 2011, 04:07 PM
Hi Nick,
Thanks again for making the post. Unfortunately, It will not be possible to mock Assembly.ReflectionOnlyLoad since it is already intercepted by the test system and as CLR profiler can not intercept a member that is already invoked.

However, Assembly is an abstract class and there are virtual methods and properties that you can easily mock using JustMock since virtual types can be mocked via proxy.


Hope this information is useful.

Kind regards,
Mehfuz
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Allen
Top achievements
Rank 2
Iron
Veteran
answered on 05 Oct 2011, 11:31 PM
Example please?
0
Ricky
Telerik team
answered on 10 Oct 2011, 02:27 PM
Hi Allen,

Thanks again for bringing up the question. However to reproduce this locally , i would recommend you to take a look into this following post where without the initialization it fails to intercept the call that is already invoked. This happens to static / concrete method where it should be intercepted during OnJITCompilationStarted and can be done once.

http://www.telerik.com/community/forums/justmock/general-discussions/mocks-failing-to-work-correctly-in-large-test-runs.aspx

Here in the above example when Foo.StaticCall() will be invoked without the initialization the original test will fail since we can't use .net profiler to intercept it when it is already JIT compiled.

Similar thing is happening for the Assembly.ReflectionOnlyLoad where although i have arranged it. It never calls the interceptor thus fails (called by the .net profiler).

Hope that answers your question.


Kind regards,
Mehfuz
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
General Discussions
Asked by
Nick
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Allen
Top achievements
Rank 2
Iron
Veteran
Share this question
or