开发者

How to test if a fluent service method is called

I have a security rule that a newly registered user has full permissions over their own user entity. I'm using Rhino.Security and the code works fine, but I want to create a unit test to make sure the appropriate call is made to setup the permission. Here is a simplified verison of the code:

public User Register(UserRegisterTask userReg开发者_开发百科istrationTask) {
   User user = User.Create(userRegistrationTask);
   this.userRepository.Save(user);

   // Give this user permission to do operations on itself
   this.permissionsBuilderService.Allow("Domain/User")
       .For(user)
       .On(user)
       .DefaultLevel()
       .Save();

   return user;
}

I've mocked the userRepository and the permissionBuilderService but the fluent interface of the permissionBuilderService requires different objects to be returned from each method call in the chain (i.e. .Allow(...).For(...).On(...) etc). But I can't find a way to mock each of the objects in the chain.

Is there a way to test if the permissionBuilderService's Allow method is being called but ignoring the rest of the chain?

Thanks Dan


I also ran into this and ended up wrapping the Rhino Security functionality in a service layer for two reasons:

  1. It was making unit testing a real PITA and after spending a couple of hours hitting my head against a brick wall, this approach allowed me to mock this layer far more easily.
  2. I started to feel that Rhino Security was being coupled very tightly to my controller (my application uses MVC). Wrapping the calls in another layer allowed me looser coupling to a specific security implementation and will allow me to easily swap it out with another - if I so choose - in the future.

Obviously, this is only one approach. But it made my life much easier...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜