How do I disable PostSharp when running unit tests?
I want my nunit tests not to apply any of my PostSharp aspects, so I can isolate my methods. Can this be done somehow in the Test Fixure Setup, or can it only be done at the project level?
You can set the "SkipPostSharp" flag in the test build so that it doesn't get compiled into your binaries in the first place.
You can have a static flag in your aspect to enable / disable it and check the status of the flag in your aspect implementation.
Then, in your unit test setup, disable the static flag.
eg.
public static bool On = true;
...
public override void OnInvoke(MethodInterceptionArgs args)
{
if (!CacheAttribute.On)
{
args.ReturnValue = args.Invoke(args.Arguments);
}
If you are using Typemock in your unit tests, you can use something like
MyAspect myAspectMock = Isolate.Fake.Instance<MyAspect>(Members.MustSpecifyReturnValues);
Isolate.Swap.AllInstances<MyAspect>().With(myAspectMock);
This allows you to control which tests are used in aspects and which are not, allowing you to test the method itself and with advice.
Presumably there will be a similar mechanism with other mocking frames