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?

Finster
  • 499
  • 1
  • 7
  • 25

1 Answers1

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")]
Mike Lowery
  • 2,630
  • 4
  • 34
  • 44
Oleg Sych
  • 6,548
  • 36
  • 34
  • 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? – Ian Griffiths Jan 31 '13 at 16:44
  • 3
    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")] – Oleg Sych Feb 01 '13 at 20:43
  • 6
    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! – John Gardner Nov 08 '13 at 22:39
  • 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")] – Ziggler Feb 20 '19 at 22:47