Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking internal classes with Moq for unit testing

Say I have a class "ClassA", which has a dependency on a class "ClassB" (injected into the constructor of ClassA). I want to mock ClassB so that I can test ClassA in isolation. Both classes are internal.

Correct me if I'm wrong but it looks like Moq can only mock a class if it is public, it has a public parameterless constructor, and the methods to be mocked are public virtual. I don't really want to make these classes publicly visible. Am I missing something with Moq, or is it just not suitable for what I want to do?

I guess I could create an interface (say "IClassB") that ClassB implements, inject that into ClassA, and mock the interface instead. ClassB can still be internal (although I realise the interface methods would have to be public). While this would work, I feel uneasy about creating lots of interfaces, whose only purpose is to support unit test mocking. Thoughts?

like image 864
Andrew Stephens Avatar asked Jul 10 '13 11:07

Andrew Stephens


People also ask

Can you mock a class with Moq?

You can use Moq to create mock objects that simulate or mimic a real object. Moq can be used to mock both classes and interfaces. However, there are a few limitations you should be aware of. The classes to be mocked can't be static or sealed, and the method being mocked should be marked as virtual.

What can be mocked with Moq?

Using Moq, you can mock out dependencies and make sure that you are testing the code in isolation. Moq is a mock object framework for . NET that greatly simplifies the creation of mock objects for unit testing.

How do you mock a method in Moq?

First, we instantiate the FakeDbArticleMock class and indicate which setup we want to use for this test. Then, it is necessary to instantiate the repository we want to test and inject the mock instance into it. Finally, we call the method we are testing and assert the results.

Can we mock private methods using Moq?

Moq supports mocking protected methods. Changing the methods to protected , instead of private , would allow you to mock their implementation.


2 Answers

You could make internals visible to Moq by adding InternalsVisibleToAttribute in your project's assembly.cs, like this:

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] 

Why "DynamicProxyGenAssembly2" and not "Moq"? It's the name of dynamic assembly created to contain dynamically generated proxy types (all of this is handled by yet another library, Castle's DynamicProxy) which is used by Moq. So you expose types to dynamic proxy assembly, not to Moq itself.

But, what's the point of mocking class if there's no overridable member? You won't mock anything and all calls will use actual implementation. Your second solution,

I guess I could create an interface (say "IClassB") that ClassB implements, inject that into ClassA, and mock the interface instead.

is what I would normally do. Its purpose is much more than "to support unit test mocking" - it helps you build losely coupled components, which is always something worth aiming for.

like image 104
k.m Avatar answered Sep 16 '22 15:09

k.m


Also, you can add this into .csporj file.

<ItemGroup>   <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">   <_Parameter1>DynamicProxyGenAssembly2</_Parameter1>   </AssemblyAttribute> </ItemGroup> 
like image 30
OxQ Avatar answered Sep 18 '22 15:09

OxQ