Making internal types accessible in Moq

When unit testing, you may be trying to test an internal interface or class. This is often done by adding the InternalsAccessibleTo assembly attribute to your your class library which makes your internal types visible to your unit test assembly. However, if you're using Moq, you may still run into the error:

Test method Listmill.Testing.Engine.Services.FlagServiceTests.Flag_ValidFlag_ReturnsTrue threw exception:  
Castle.DynamicProxy.Generators.GeneratorException: Type Listmill.Engine.Services.IFlagHandler is not public. Can not create proxy for types that are not accessible.  

In order to get Moq to work with internal types, you will need to also add an InternalsAccessibleTo for Moq's internal proxy generator

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

Once that is added, you unit tests should run just dandy.

comments powered by Disqus