Search code examples
unit-testingmoqroleprovider

Unit testing custom RoleProvider with Moq?


I created a custom RoleProvider in a custom library. I would like to unit test it. Via Moq I created a fake HttpContextBase. How to pass this to the to be tested RoleProvider?

The Identity is a custom test implementation class. This works fine. I only don't know how to pass in the fake context in my provider. This is not an MVC application but standard Webforms if that's information needed.

Grz, Kris.


Solution

  • You could use Dependency Injection (DI) and pass it through your custom RoleProvider's constructor.

    public MyRoleProvider(HttpContextBase httpContext)
    {
        // ...
    }
    

    This would allow you to pass the Moq instance via the constructor.