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

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-01 08:51:15

问题


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?


回答1:


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.

[assembly: InternalsVisibleTo("FileSystem.Fakes")]
[assembly: InternalsVisibleTo("FileSystem.Tests")]

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")]


来源:https://stackoverflow.com/questions/13573112/how-to-mock-isolate-internal-classes-in-vs-2012-with-fakes-framework-shims

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!