非code产生的转发垫片用于测试私有方法 [英] Non-code-generated forwarding shim for testing private methods

查看:187
本文介绍了非code产生的转发垫片用于测试私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一般情况下,我的设计的类以这​​样的方式,以不要求访问下身用于测试目的。一个<一href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx"相对=nofollow> InternalsVisibleTo 也可以协助。

In general, I design classes in such a manner as to not require access to privates for testing purposes. An InternalsVisibleTo can also assist.

不过,我目前用codeBase的,有已成为依赖的的在VSTS 私人存取机制(即使用 VS codeGenAccessors 来生成 * _存取类已转发了使用反射来调用私人成员(以及可选内部一类的人也是如此)。

However, I'm currently dealing with a codebase that has a few area which have become reliant on the private accessors mechanism in VSTS (i.e., using VSCodeGenAccessors to generate *_Accessor classes which have forwarding that use reflection to invoke private members (and optionally internal ones too) on a class.

所以,我已经得到了code这样的:

So I've got code like:

ClassUnderTest target = new ClassUnderTest();
var accessor = ClassUnderTest_Accessor.AttachShadow( target );
accessor.PrivateMethod();
Assert.True( accessor._privateMethodWasCalled);
accessor.PrivateProperty = 5;
Assert.Equal( accessor.PrivateProperty, 5);

(是的,充斥着反模式 - 但请不要拍的使者)

(Yes, riddled with antipatterns - but please dont shoot the messenger)

我有一些问题,这一点:

I have a number of problems with this:

  1. 我希望能够澄清,我需要我私处
  2. 在我preFER不被调用对话(是的,我是一个CRaholic)
  3. 在我preFER不涉及code一代的图片

所以,我希望能够改变上述code到是这样的:

So I'd like to be able to transform the above code to something like:

var target = new ClassUnderTest();
IClassUnderTestInterface accessor = Shadow.Create<IClassUnderTestInterface>( target );
accessor.PrivateMethod();
Assert.True( accessor._privateMethodWasCalled);
accessor.PrivateProperty = 5;
Assert.Equal( accessor.PrivateProperty, 5);

由于只有出现如下界面坐在我的测试组件,并没有产生code或自定义生成步骤: -

With only the following interface sitting in my test assembly, and no generated code or custom build steps :-

interface IClassUnderTestInterface
{
   int PrivateProperty {get; set;}
   bool _privateMethodWasCalled {get; }
   void PrivateMethod();
}

从那里,我可以用$ C $粉碎或Ctrl公里生成新的阴影方法到接口只有一个按键。

From there, I'd be able to use CodeRush or Ctrl K M to generate new shadow methods onto the interface with only a keystroke.

丢失位将有一个方法我Shadow.Create&LT; I&GT;(对象o)这将   1.生成一个动态代理实现接口   1.验证对象 0 来包裹了所有的接口决定成员   1. bnous:管理属性的转发重新presenting领域(即'_privateMethodWasCalled'的情况)正确

The missing bit would be having a method I Shadow.Create<I>( Object o) which would 1. generate a dynamic proxy that implements the interface 1. verify that the object o to be wrapped has all the members dictated by the interface 1. bnous: manage the forwarding of properties representing fields (i.e., the `_privateMethodWasCalled' case) correctly

因此​​,没有人知道,实现这样的一个库(或者觉得有足够的无聊写呢?)

So, does anyone know a library that implements something like this (or feel bored enough to write it?)

一个明显的缺点是,优尔不知道是否与ClassUnderTest心不是commpatible直到运行时的界面,不过没关系,因为这只能是做检查。此外AIUI,私人访问机制也需要触发重新编译的同步东西了,不时。

One obvious drawback is that youo dont know if the interface isnt commpatible with the ClassUnderTest until runtime, but that's OK as this would only be for tests. Also AIUI, the private accessors mechanism also requires the triggering of a recompile to sync stuff up from time to time.

或者是有没有办法更好的方法,我失踪? (记住我不希望走毯ugrading人士兵内部或公共和不想有重写工作code)

Or is there a way better approach I'm missing? (Remembering I dont want to go blanket ugrading al privates to internal or public and dont want to have to rewrite working code)

使用xUnit.net,.NET 3.5;打开使用任何动态代理库或其他

Using xUnit.net, .NET 3.5; Open to using any dynamic proxy library or other

推荐答案

我结束了路由围绕这一切的东西( InternalsVisibleTo ,单独的测试项目,测试参考,私人访问的MSBuild的影子任务/ publicize.exe失败中随机生成,使用反射来访问私处)通过合并测试分为主体工程,使这需要从测试访问任何内部)。另请参见<一个href="http://stackoverflow.com/questions/1443278/xunit-net-test-stripper-to-remove-test-$c$c-embedded-in-binaries-prior-to-deploy">http://stackoverflow.com/questions/1443278/xunit-net-test-stripper-to-remove-test-$c$c-embedded-in-binaries-prior-to-deploy

I ended up routing around all this stuff (InternalsVisibleTo, separate test projects, Test References, private accessors, the MSBuild Shadow task / publicize.exe failing randomly in builds, using reflection to access privates) by merging the tests into the main projects and making anything that needed to be accessed from tests internal). See also http://stackoverflow.com/questions/1443278/xunit-net-test-stripper-to-remove-test-code-embedded-in-binaries-prior-to-deploy

这篇关于非code产生的转发垫片用于测试私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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