views:

52

answers:

0

I'm trying to write some tests for an MVC application we're developing. We have a BaseController class that contains the following:

public class BaseController : Controller
{
    protected string UserRole { get; private set; }

We then have a controller that inherits from the BaseController:

public class CustomFieldController : BaseController

I've generated private accessors for both classes (just regenerated them a few minutes ago). In one of my unit tests for CustomFieldController I want to set the UserRole, so I've got the following code:

CustomFieldController controller = new CustomFieldController();
CustomFieldController_Accessor accessor = new CustomFieldController_Accessor( 
    new PrivateObject( controller, new PrivateType( typeof( BaseController ) ) ) );

accessor.UserRole = "OTHER";

Every time I try to run this test it throws an exception on the last line stating:

The member specified (CustomFieldEdit) could not be found. You might need to regenerate your private accessor, or the member may be private and defined on a base class. If the latter is true, you need to pass the type that defines the member into PrivateObject's constructor.

As far as I can tell, I've done what it says. Not only have I recently regenerated the private accessor, but I am passing the type that defines the member into PrivateObject's constructor.

Any thoughts as to what I'm missing here? I know I can make it work by taking the "private" off the property setter, but I'd rather not do that if I can avoid it (don't want subclass implementers thinking they can inject a value into that property).