如何使用 Fakes Framework shims 在 VS 2012 中模拟/隔离内部类? [英] How to mock/isolate internal classes in VS 2012 with Fakes Framework shims?

查看:30
本文介绍了如何使用 Fakes Framework shims 在 VS 2012 中模拟/隔离内部类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,问题是我的程序集中有一堆内部类供我要测试的类使用.由于访问器已从 VS2012 中删除,我可以使用 [InternalsVisibleTo] 并且效果很好......除非当我尝试对内部类进行 shimify 时,它们对 Fakes 框架不可见.我还应该注意,我正在处理的特定类是一个带有静态方法的静态类,我真的不想重构所有内容以使用接口(和使用存根),因为组织中对这种重构级别有抵抗力.

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.

有没有办法让这些内部成员可以在 Visual Studio 2012 中的 shims 上下文中使用,而无需(字面上)连接所有内容?

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

推荐答案

下一页描述了如何使内部类型对生成的 fakes 程序集可见.http://msdn.microsoft.com/en-us/library/hh708916.aspx#bkmk_internal_types

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

说的是:

Fakes 代码生成器将为生成的 Fakes 程序集可见的类型生成填充类型和存根类型.要使填充程序集的内部类型对 Fakes 和您的测试程序集可见,请将 InternalsVisibleToAttribute 属性添加到填充后的程序集代码,使生成的 Fakes 程序集和测试程序集可见.

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.

因此您需要一个 InternalsVisibleToAttribute 来授予您的测试程序集访问目标程序集的内部类型的权限.换句话说:

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

这篇关于如何使用 Fakes Framework shims 在 VS 2012 中模拟/隔离内部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆