Mock internal abstract class

4 posts, 0 answers
  1. Eric
    Eric avatar
    2 posts
    Member since:
    Aug 2008

    Posted 21 May 2012 Link to this post

    I'm programming with the Windows Azure SDK and want to Mock the LocalResource object in my unit tests. Problem is that LocalResource is an abstract class that defines property set as internals. The Mock object/proxy does not implement these property set and an exception is thrown at runtime. Looks like a loophole in the language definition. How to write my unit tests?

    var resource = Mock.Create<LocalResource>();
    Mock.Arrange(() => RoleEnvironment.GetLocalResource("workingfolder")).Returns(resource);
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 24 May 2012 Link to this post

    Hi Eric,
    Thanks again for contacting us.

    If you are trying to mock non-virtual internal methods for an abstract class then you need to make sure that JustMock profiler is running properly. You can check it through this line:

    Assert.True(Mock.IsProfilerEnabled)

    If everything is configured properly then you can easily write a test  as shown below:
    [TestMethod]
    public void TestMethod1()
    {
         var foo = Mock.Create<Foo>();
          
         Mock.Arrange(() => foo.Submit()).DoNothing();
     
         foo.Submit();
     
    }
     
     
     public abstract class Foo
     {
         internal void Submit()
         {
             throw new NotImplementedException();
         }
     }

    However, if you are mocking internal virtual methods then i would recommend you to take a look at the following post:

    http://www.telerik.com/help/justmock/basic-usage-mock-internal-types-via-proxy.html


    Kind Regards
    Ricky
    the Telerik team

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

  3. DevCraft R3 2016 release webinar banner
  4. Eric
    Eric avatar
    2 posts
    Member since:
    Aug 2008

    Posted 25 May 2012 Link to this post

    Hello Ricky,

    Thanks for you effort. I tried your profiler check code. Unfortunately it didn't fix my issue.
    The problem is not that I cannot call an internal method, but an exception is thrown when I create a Mock at runtime. This is because the LocalResource class is defined as an abstract class (which should be implemented) with internal property setters (which should be implemented internal)
    When I manualy create a class that inherits from LocalResource it will complain about not implementing the internal property setters and when I do the compiler will complain that I did. Looks like a loophole in the C# language. Remember: the LocalResource class is in a Microsoft assembly and not my work.
    public abstract class LocalResource
    {
        public abstract int MaximumSizeInMegabytes { get; internal set;}
        public abstract string Name { get; internal set; }
        public abstract string RootPath { get; internal set;}
    }

  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 29 May 2012 Link to this post

    Hi Eric,
    Thanks again for bringing up the question.

    If you have access to the source of assembly that contains LocalResource class then in order mock it, please include the following line in AssemblyInfo.cs:

    [assembly: InternalsVisibleTo("Telerik.JustMock,PublicKey=0024000004800000940000000602000000240000525341310004000001000100098b1434e598c6" +
    "56b22eb59000b0bf73310cb8488a6b63db1d35457f2f939f927414921a769821f371c31a8c1d4b" +
    "73f8e934e2a0769de4d874e0a517d3d7b9c36cd0ffcea2142f60974c6eb00801de4543ef7e93f7" +
    "9687b040d967bb6bd55ca093711b013967a096d524a9cadf94e3b748ebdae7947ea6de6622eabf" +
    "6548448e")]

    Otherwise .net framework does not allow intercepting an abstract internal member. Please also take a look into the following post for more information on how to mock a internal member using InternalsVisibleTo attribute .

    http://www.telerik.com/help/justmock/basic-usage-mock-internal-types-via-proxy.html 

     
    Kind Regards
    Ricky
    the Telerik team

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

Back to Top