In the previous section, we have seen how to test the aspect behavior itself. Now, let's see how we can test that the aspect has been applied to the expected set of targets. This can also be called testing the pointcut.
Why test that the aspect has been properly applied?
You may need to test whether an aspect has been applied to specific targets for one of the following reasons:
The aspect is applied using non-trivial regular expressions with MulticastAttribute.
The aspect is silently filtered out using CompileTimeValidate(MethodBase).
The aspect is applied using an IAspectProvider.
Testing that the aspect behavior is exhibited
The most obvious way to test that the aspect has been applied to an element of code is to execute that code and ensure that the code actually exhibits the aspect behavior. This approach does not differ from the one described in section Testing Run-Time Logic.
Testing that the aspect custom attribute is present
You can check that an aspect has been applied to a target by reflecting the custom attributes present on this element of code.
However, custom attributes representing aspects are stripped by default. If you want PostSharp to emit custom attributes, follow instructions of section Reflecting Aspect Instances at Runtime.
Note
Aspects added by IAspectProvider are not represented by custom attributes, so their presence cannot be tested by this approach.
Parsing the PostSharp symbol file
PostSharp generates a symbol file named bin\Debug\MyAssembly.psssym, where MyAssembly is the name of the assembly. In theory, you could use this file to determine which elements of code have been modified by aspects in your project.
Caution
The PostSharp symbol file format is undocumented and unsupported. It means that PostSharp support team cannot answer questions related to this file format.