views:

201

answers:

1

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.

A: 

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.

Mark Seemann
And then how to handle it?
XIII
Save it as a member variable.
Mark Seemann
Sorry I don't get it. The roleprovider can't be hassled with (codewise). Is there another workaround possible?
XIII
If you can't create a RoleProvider via code, you may alternatively be able to use a ServiceLocator to get an instance of HttpContextBase for you from the inside. It's not as nice, but sometimes it is necessary.
Mark Seemann