Unit test class inherited from ContextBoundObject and decorated with ContextAttribute

I am trying to modify unit tests to some existing code base. Both the class and the method that I want to use unit test are decorated with custom attributes that are inherited from ContextBoundObject and ContextAttribute. I do not want them to run as part of a unit test.

The only solution I came across is to compile the attribute when I want to unit test. I really don't like this solution, and I would prefer to either replace it with a mocking attribute at runtime, or prevent a more convenient way to use this attribute.

How are you unit test code that has class and method attributes that inherit from ContextBoundObject and ContextAttribute that you don't want to run as part of unit test?

Thanks in advance.

+4
source share
1 answer

Classes that inherit from ContextBoundObject methods by passing messages, rather than a traditional stack-based execution model. This ability is used as the basis for remote and COM communications. One interesting option is that it becomes possible to intercept method calls using attributes, which allows you to use a kind of β€œbad AOP person”.

One way to get rid of the logic provided by the attributes would be to create a configuration file to override which class is created with the new class. This feature is only available for classes that inherit from MarshalByRef, which is the base class for ContextBoundObject.

Another option would be to instantiate the class using reflection. That is, to find a specific ConstructorInfo and call it to create an instance. When you are just a β€œnew” instance, the call is redirected to Activator.CreateInstance, which probably gives you a proxy object for the actual class. Creating an object directly can get around this, although this is a shot in the dark.

+2
source

Source: https://habr.com/ru/post/1306313/


All Articles