In this post I will show mocking a member call inside a PostSharp aspect. There were previously compatibility issues between both of the tools running side by side which is now been officially fixed with the most recent release of that tool (>= 184.108.40.206-1594). For those who don’t know what PostSharp is all about, it is a tool that lets you write aspects easily than you can imagine.
Personally, it took me just minutes to get started with a aspect that will authorize a call before invoking it. Consider that you want to authorize an asp.net MVC controller action and if you ever look into the code you will see that it validates the currently logged in user’s identity.
In the simplest way I have an Identity class that has a IsAuthenticated property which is not implemented for sure:
Next we have an Inventory class that places an order for an authorized user:
Now AuthorizeAttribute is implemented on MethodInterceptionAspect where we can write useful code during the execution of a particular action (return to login page if not authorized, etc). In this case it just skips call for invalid identity.
Here to note that SerializableAttribute is a required attribute by PostSharp. Once you compile the code it will actually write the necessary hooks inside the method based on the aspects you have declared.
Next is a simple test using MSpec where I ensured that if the identity is valid then it places the order to an inventory.
Here I have mocked the static Identity call using the Justmock that intercepts it on top the PostSharp hooks during authorization. Further I used the PostSharp starter edition for the purpose.
you can find the sample project here :
Happy coding ~~
Copyright © 2017, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks or appropriate markings.