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

Mock internal abstract class

3 Answers 455 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 21 May 2012, 09:48 PM
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);

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 24 May 2012, 06:21 PM
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 >>

0
Eric
Top achievements
Rank 1
answered on 25 May 2012, 07:49 AM
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;}
}

0
Ricky
Telerik team
answered on 29 May 2012, 07:54 PM
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 >>

Tags
General Discussions
Asked by
Eric
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Eric
Top achievements
Rank 1
Share this question
or