How to mock/isolate internal classes in VS 2012 with Fakes Framework shims?
Asked Answered
S

1

10

So, the issue is that I have a bunch of internal classes in my assembly that are used by the class I want to test. Since accessors have been removed from VS2012, I'm fine with using [InternalsVisibleTo] and that works great... except when I try to shimify my internal classes, they are not visible to the Fakes framework. I should also note that the particular class I am dealing with is a static class with static methods, and I don't really want to refactor everything to use interfaces (and using stubs) because of resistance in the organization to that level of refactoring.

Is there a way to make these internal members usable by the shims context in Visual Studio 2012 without resorting to interfacing (literally) everything?

Shawntashawwal answered 26/11, 2012 at 21:2 Comment(0)
V
25

The following page describes how to make internal types visible to the generated fakes assembly. http://msdn.microsoft.com/en-us/library/hh708916.aspx#bkmk_internal_types

Which says:

The Fakes code generator will generate shim types and stub types for types that are visible to the generated Fakes assembly. To make internal types of a shimmed assembly visible to Fakes and your test assembly, add InternalsVisibleToAttribute attributes to the shimmed assembly code that gives visibility to the generated Fakes assembly and to the test assembly.

So you need an InternalsVisibleToAttribute that grants your test assembly access to the internal types of the target assembly. In other words:

[assembly: InternalsVisibleTo("TargetAssembly.Fakes")]
[assembly: InternalsVisibleTo("TestAssembly")]
Vest answered 26/11, 2012 at 21:35 Comment(4)
It does indeed say that, and yet when I've tried this, none of my internal interfaces show up. I only get stubs for public interfaces. I know the original question was about shims - have you been able to get this to work for stubbing internal interfaces?Stace
We are in the process of updating the MSDN page. You also need an InternalsVisibleToAttribute to allow your test assembly access the internal types from the target assembly. In other words, you need: [assembly: InternalsVisibleTo("TargetAssembly.Fakes")] [assembly: InternalsVisibleTo("TestAssembly")]Vest
important note from that msdn page that I missed: The Fakes framework uses the same key to sign all generated assemblies, so the public key if signing should ALWAYS be PublicKey=0024000004800000940000000602000000240000525341310004000001000100e92decb949446f688ab9f6973436c535bf50acd1fd580495aae3f875aa4e4f663ca77908c63b7f0996977cb98fcfdb35e05aa2c842002703cad835473caac5ef14107e3a7fae01120a96558785f48319f66daabc862872b2c53f5ac11fa335c0165e202b4c011334c7bc8f4c4e570cf255190f4e3e2cbc9137ca57cb687947bc if you're signing stuff! It took me forever to catch this!Merchantable
In my case I have MyLibrary.dll and testing this in MyUnitTestProject using Shims. I got warning saying type is not visible to exported or assembly. So I opened AssemblyInfo.cs for MyLibrary project and gave [assembly:InternalsVisibleTo("MyLibrary.Fakes")]Halftimbered

© 2022 - 2024 — McMap. All rights reserved.