4

I'm have that Controller:

[Authorize(Roles = "Administrator")]
public class ApuradorController : Controller
{
    private readonly Questiona2011Context _context;
    private readonly AuthenticationService _authenticationService;

    public ApuradorController(Questiona2011Context context, AuthenticationService authenticationService)
    {
        this._context = context;
        this._authenticationService = authenticationService;
    }

    ...
}

I'm using RoleProvider. How I can testing Roles and Authorization in my Controller?

2 Answers 2

7

Filters (for example, your authorizeattribute) are designed to be independent of the controllers and action methods. when you are writing unit tests for you action methods, filters are simply ignored, they are just attributes. But, you do not have to test them when you are unit testing your controllers and actions! If you want to test how your app works alltogether, you must write ui automation tests.

Update: Steven Sanderson have great explanation of that in his book pro asp.net mvc 2 framework. In fact, I repeated his words above

Sign up to request clarification or add additional context in comments.

1 Comment

Depending on how your filters are implemented you should be able to write tests for them directly. Then in your unit tests for your controllers just test their behaviors since they are going to be ignored as objectbox pointed out.
1

I believe this is for integration test again. Watin ( http://watin.org/ ) is a framework for Web integration test.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.