问题
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?
回答1:
You could set the 'SkipPostSharp' flag on the test build, so that it is not compiled into your binaries in the first place.
回答2:
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);
}
回答3:
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
来源:https://stackoverflow.com/questions/14691554/how-do-you-disable-postsharp-when-running-unit-tests