This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET 3.5+

Mocking Internal Types with Moq

To perform successful unit testing classes should be isolated from their dependencies. When the dependencies are internal and tests are in a separate project, this can be achieved using a combination of Moq and C# friend assemblies.

Mocking Internal Types

Mocking using the Moq framework provides a simple way to create test doubles that can be provided to a class under test using dependency injection. The mock objects can be pre-programmed with responses to specific calls, letting you isolate the class under test and simplify your unit testing.

In most cases you will structure your solution so that the tests are in a separate project to the types being tested. This allows you to ship the completed application without delivering the test code to your end users. The separate test project can reference the application code and access its public types, testing all of the code through public interfaces.

If you are creating a class library and you do not want all of the types within it to be visible to the library's end users, you may decide to make some classes internal. These can still be used as dependencies but are hidden under normal use. Testing these classes and injecting internal dependencies from a separate test project requires an additional step. The test project must be configured to be a friend assembly of the assembly being tested.

As an example, let's say that you have a project that you wish to test, named "ProjectUnderTest", that contains internal classes. Within the same solution you have a project containing tests that you have named, "Tests". Unless configured otherwise, the internal types of ProjectUnderTest are invisible to the Tests project. Using the techniques described in the article, "C# Friend Assemblies", you can make the internal types visible to the test project. However, if you wish to mock those internal types using Moq, you must also make an assembly from the Moq framework a friend assembly of the project being tested. The name of this assembly is "DynamicProxyGenAssembly2".

If you are not using signed assemblies, you would add the following two attributes to your project.

[assembly: InternalsVisibleTo("Tests")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

NB: The attributes should be added to the assembly.cs file of the project under test, not of the project containing the tests.

Mocking Internal Types in Signed Assemblies

If the assembly that you are developing is signed, the InternalsVisibleTo attributes must be updated to hold the public key values for the test assembly and the DynamicProxyGenAssembly2 assembly. You can obtain the public key for your test assembly using the sn.exe tool, as described in the C# Friend Assemblies article. The DynamicProxyGenAssembly2 public key is shown in the updated attribute below:

[assembly: InternalsVisibleTo("Tests,PublicKey=00240000048000009400000006020000002400005253413100040000010001009545b820b40d44286e8fb5e5ec0e679d76bcc324fa15abd1f6d8e3d25dc2ee615c442b92fb1db774d54fa2aa4997311ed1d77dc4b641ec5be7d0f8fc7c92489e42139823c23798f3d04fae934ca9918c49335454fcaa0882f11a27164c719ece35ddf633affcebeaafd7c9312cad9aee05870f940993cb38cf6162c7ac92eacd")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
11 December 2011