1

I have my own custom Authorize Attribute and I am trying to check my controller methods to see if they have the correct roles in place. Now my custom authorize tag has database code in it.

The ways I am mocking it up don't seem to work since the reflection stuff I found seems to to just pass no arguments so my default constructor in the Authorize Attribute gets hit creating a new service layer object that creates a repository object(that kills the unit test).

 var indexAction = typeof(Controller).GetMethod(method);
        var authorizeAttributes = indexAction.GetCustomAttributes(typeof(AuthorizeAttribute), true);

        //Assert
        Assert.That(authorizeAttributes.Length > 0, Is.True);

        foreach (AuthorizeAttribute att in authorizeAttributes)
        {
            Assert.That(att.Roles, Is.EqualTo(roles));
        }

Constructors of my AutorizeAttribute

  public MyAuthorize()
    {
        authorize = new ServiceLayer();
    }

    public MyAuthorize(IServicelayer layer)
    {
        authorize = layer;
    }

the reflection stuff keeps calling my default constructor. How can I pass in a mock service layer or something?

Thanks

2
  • Why do you have an overloaded constructor for your attribute at all? You will never be able to use the overloaded constructor when applying the attribute, so it doesn't do any good. I suspect that it exists entirely for the sake of Testability, but if you can't use it in production scenarios as well, it indicates a code smell to me. Commented Oct 5, 2009 at 7:25
  • I disagree, Mark. It's a perfectly acceptable method of IoC and DI used for testing. Doesn't matter that you don't take that route during live execution. Commented Oct 5, 2009 at 10:03

2 Answers 2

1

Have you looked at some of the Mocking Frameworks? I've used these to fake the http context etc in the past.

Here's another Stack Overflow post that might be able to help you...

https://stackoverflow.com/questions/37359/what-c-mocking-framework-to-use

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

Comments

0

I don't think the problem is with your code but what you are trying to test. What determines the roles that the attribute has?

If you are retrieving the roles from your service layer based on something passed into the attribute, your tests should confirm that the attribute exists on the action it is protecting ( part of the controller tests ), the appropriate calls are made to your service layer from the attribute ( part of the attribute tests ), and that the service layer returns the appropriate values for a specific request ( part of the controller tests ).

To ensure all of the parts work together, you will need to use integration tests that essentially mimic the entire request pipeline - something like Steve Sanderson's MvcIntegrationTest should simplify this http://blog.codeville.net/2009/06/11/integration-testing-your-aspnet-mvc-application/

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.