简体   繁体   English

从ContextBoundObject继承并用ContextAttribute装饰的单元测试类

[英]Unit test class inherited from ContextBoundObject and decorated with ContextAttribute

I am trying to retrofit unit tests on to some existing code base. 我正在尝试将单元测试改造为一些现有的代码库。 Both the class and method I want to unit test is decorated with custom attributes that are inherited from ContextBoundObject and ContextAttribute. 我要进行单元测试的类和方法都装饰有从ContextBoundObject和ContextAttribute继承的自定义属性。 I dont want them to run as part of the unit test. 我不希望它们作为单元测试的一部分运行。

The only solution I have come up with is to compile the attribute out when I want to unit test. 我想出的唯一解决方案是在我要进行单元测试时将属性编译出来。 I dont really like this solution and would prefer to either replace it with a mocked attribute at runtime or prevent the attribute from running in a more elegant way. 我真的不喜欢这种解决方案,而是希望在运行时将其替换为模拟属性,或者阻止该属性以更优雅的方式运行。

How do you unit test code that has class and method attributes that inherit from ContextBoundObject and ContextAttribute that you dont want to run as part of a unit test? 如何对具有从ContextBoundObject和ContextAttribute继承而又不想作为单元测试的一部分继承的类和方法属性的代码进行单元测试?

Thanks in advance. 提前致谢。

Classes that inherit from ContextBoundObject execute methods by passing messages rather than the traditional stack-based execution model. 从ContextBoundObject继承的类通过传递消息而不是传统的基于堆栈的执行模型来执行方法。 This ability is used as the foundation for remoting and COM interop. 此功能用作远程和COM互操作的基础。 One of the interesting options is that it becomes possible to intercept method calls using attributes, which allows for a kind of "poor mans AOP". 有趣的选项之一是可以使用属性来拦截方法调用,这允许一种“穷人AOP”。

One of the options for getting rid of the logic provided by the attributes would be to create a configuration file to override which class gets created when you new your class. 摆脱属性提供的逻辑的一种方法是创建一个配置文件,以覆盖在新建类时创建的类。 This ability is only available to classes inheriting from MarshalByRef, which is the base class for ContextBoundObject. 此功能仅适用于从MarshalByRef继承的类,该类是ContextBoundObject的基类。

Another option might be to create an instance of the class using reflection. 另一个选择可能是使用反射创建类的实例。 That is, to locate a particular ConstructorInfo and invoke it to create an instance. 也就是说,找到一个特定的ConstructorInfo并调用它来创建一个实例。 When you simply "new" an instance the call is routed to Activator.CreateInstance, which probably gives you a proxy object for the actual class. 当您简单地“新建”一个实例时,调用将被路由到Activator.CreateInstance,它可能会为您提供实际类的代理对象。 Creating the object directly might bypass this, although it's a shot in the dark. 尽管是在黑暗中拍摄,但直接创建对象可能会绕过此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用Authorize属性修饰的单元测试操作 - unit test actions decorated with Authorize attribute NUnit测试用例不是从继承的类运行 - NUnit test cases not run from inherited class 在MSTest中忽略来自泛型基类的继承测试类 - Inherited test class from generic base is ignored in MSTest 如何对从 NetworkStream 派生的 class 进行单元测试? - How to unit test a class derived from NetworkStream? MVC中的单元测试类 - Unit test Class in MVC 对继承的 class 的最小起订量测试始终返回 null - Moq test on inherited class always returns null c#WebApi如果使用具有特定参数的授权属性修饰控制器操作,如何进行单元测试 - c# WebApi how to unit test if a controller action is decorated with an authorization attribute with specific arguments 如何对装饰有 ServiceFilterAttribute、自定义 ActionFilter 实现的 controller 进行单元测试 - How do I unit test a controller that is decorated with the ServiceFilterAttribute, custom ActionFilter implementation 在单元测试中模拟班级中的班级 - Mock a Class in a Class in a Unit Test 从接口到类继承了一个属性 - Inherited a property from interface to class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM