Telerik blogs

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 (>= 2.1.2.8-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:

 

  1. public class Identity
  2. {
  3.     public static bool IsAuthenticated
  4.     {
  5.         get
  6.         {
  7.             throw new NotImplementedException("Mimic the code User.Identity.IsAuthenticated");
  8.         }
  9.     }
  10. }

 

Next we have an Inventory class that places an order for an authorized user:

  1. public class Inventory
  2.  {
  3.      [Authorize]
  4.      public bool PlaceOrder(Order order)
  5.      {
  6.          orders.Add(order);
  7.          return true;
  8.      }
  9.  
  10.      public bool HasOrders()
  11.      {
  12.          return orders.Count > 0;
  13.      }
  14.  
  15.      private IList<Order> orders = new List<Order>();
  16.  }

 

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.

  1. [Serializable]
  2. public class AuthorizeAttribute : MethodInterceptionAspect
  3. {
  4.     public override void OnInvoke(MethodInterceptionArgs args)
  5.     {
  6.         if (Identity.IsAuthenticated)
  7.             base.OnInvoke(args);
  8.     }
  9. }

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.

  1. [Subject(typeof(Inventory))]
  2. public class when_authorization_ensures_a_valid_identity
  3. {
  4.     Establish context = () =>
  5.     {
  6.         Mock.Arrange(() => Identity.IsAuthenticated).Returns(true);
  7.         inventory = new Inventory();
  8.     };
  9.  
  10.     private Because of = () =>
  11.     {
  12.         inventory.PlaceOrder(new Order("Talisker", 1) { OrderDate = DateTime.Now });
  13.     };
  14.  
  15.     private It should_assert_that_order_place_was_successful = () =>
  16.         inventory.HasOrders().ShouldBeTrue();
  17.    
  18.     static Inventory inventory;
  19. }

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 ~~


Comments

Comments are disabled in preview mode.