Mocking Assembly.ReflectionOnlyLoadFrom

4 posts, 0 answers
  1. Nick
    Nick avatar
    4 posts
    Member since:
    Jul 2010

    Posted 07 Apr 2011 Link to this post

    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

  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 12 Apr 2011 Link to this post

    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
  3. DevCraft R3 2016 release webinar banner
  4. Allen
    Allen avatar
    50 posts
    Member since:
    Mar 2011

    Posted 05 Oct 2011 Link to this post

    Example please?
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 10 Oct 2011 Link to this post

    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 >>

Back to Top