Mocks for internal interfaces

Maybe you’ve already seen an exception like this:

Castle.DynamicProxy.Generators.GeneratorException: Type is not public, so a proxy cannot be generated. Type: SpielWiese.IReportSender
…stack-trace…
at SpielWiese.TestSomething.ExpectReportsSend() in Program.cs: line 64

What I’ve tried is to create a mock-object with Rhino-Mock. Since this interface is only intended for usage within the assembly it’s declared as internal interface. So Rhino-Mock fails and I really don’t want to create a mock manually. The quickest and dirtiest way would be to make the interface public. But I really don’t want to leak internal stuff just for tests.

So the second approach is to make the internals visible to Rhino-Mocks.

[assembly: InternalsVisibleTo("Rhino.Mocks")]

But it will fail as well. Since Rhino uses Castle-Proxies, I tried:

[assembly: InternalsVisibleTo("Castle.DynamicProxy")]

It still won’t work. The reason for this is simple. The Mock-Factory has to generate code. The generated code is hosted in a separate assembly. So you have to make the dynamic assembly visible to your code under test.

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

This is similar with other mock frameworks or proxy-frameworks.

Source: http://ayende.com/Wiki/Rhino+Mocks+-+Internal+Methods.ashx

Demo-Code: demo-code

Tagged on: , , ,

2 thoughts on “Mocks for internal interfaces

Leave a Reply

Your email address will not be published. Required fields are marked *