How do you disable PostSharp when running unit tests?

浪尽此生 提交于 2019-12-10 20:57:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!