3

I want my nunit tests not to apply any of my PostSharp aspects so I can test my methods in isolation. Can this be done somehow in the Test Fixture Setup, or can it only be done on a per project level?

JMac
  • 523
  • 1
  • 5
  • 17

3 Answers3

3

You could set the 'SkipPostSharp' flag on the test build, so that it is not compiled into your binaries in the first place.

RJ Lohan
  • 6,497
  • 3
  • 34
  • 54
  • I was hoping of a more flexible way so I could enable aspects for some tests and disable for others, but I guess this isn't possible. To be honest I don't know if the better option is to test with aspects being applied or without. – JMac Feb 05 '13 at 10:25
  • I guess turning PostSharp off for my unit test build, then on for integration test build which runs a different set of tests is the answer. – JMac Feb 05 '13 at 10:28
  • I'd test it with the aspects in place. I've written an aspect or 2 which turn out to introduce unexpected errors in some situations. – RJ Lohan Feb 05 '13 at 21:43
  • For Visual Studio for Mac Community edition v8.10, this can be done under Project -> Options -> Compiler -> Define Symbols -> add SkipPostSharp into the ';' separated list – Piyush Khera Oct 28 '22 at 15:54
3

You can have a static flag on your aspect to toggle it on/off and check the status of the flag in your aspect implementation.

Then in your unit test setup just turn the static flag off.

e.g.

    public static bool On = true;

...

    public override void OnInvoke(MethodInterceptionArgs args)
    {
        if (!CacheAttribute.On)
        {
            args.ReturnValue = args.Invoke(args.Arguments);
        }
Brett Postin
  • 11,215
  • 10
  • 60
  • 95
0

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 what tests the aspects are used on, and which ones are not, allowing you to test the method itself, and with the advices applied.

Presumably there would be a similar mechanism with other mocking frameworks

sweetfa
  • 5,457
  • 2
  • 48
  • 62