Quantcast
Channel: Telerik Blogs
Viewing all articles
Browse latest Browse all 5210

PostSharp and JustMock side by side.

$
0
0

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. publicclassIdentity
  2. {
  3.     publicstaticbool IsAuthenticated
  4.     {
  5.         get
  6.         {
  7.             thrownewNotImplementedException("Mimic the code User.Identity.IsAuthenticated");
  8.         }
  9.     }
  10. }

 

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

  1. publicclassInventory
  2.  {
  3.      [Authorize]
  4.      publicbool PlaceOrder(Order order)
  5.      {
  6.          orders.Add(order);
  7.          returntrue;
  8.      }
  9.  
  10.      publicbool HasOrders()
  11.      {
  12.          return orders.Count > 0;
  13.      }
  14.  
  15.      privateIList<Order> orders = newList<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. publicclassAuthorizeAttribute : MethodInterceptionAspect
  3. {
  4.     publicoverridevoid 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. publicclasswhen_authorization_ensures_a_valid_identity
  3. {
  4.     Establish context = () =>
  5.     {
  6.         Mock.Arrange(() => Identity.IsAuthenticated).Returns(true);
  7.         inventory = newInventory();
  8.     };
  9.  
  10.     privateBecause of = () =>
  11.     {
  12.         inventory.PlaceOrder(newOrder("Talisker", 1) { OrderDate = DateTime.Now });
  13.     };
  14.  
  15.     privateIt should_assert_that_order_place_was_successful = () =>
  16.         inventory.HasOrders().ShouldBeTrue();
  17.    
  18.     staticInventory 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 ~~


Viewing all articles
Browse latest Browse all 5210

Trending Articles