1

I have a controller in the admin section of my site and it is decorated with the Authorize filter with the role set to admin.

[Authorize(Roles = "Admin")]
public class SubscriberController : Controller

This works great but I'd like to create a unit test to ensure that the filter is not removed. I've got this far, to validate that the Authorize filter is present.

        typeof(SubscriberController).Should()
            .BeDecoratedWith<AuthorizeAttribute>(
                "Subscriber controller users must be admins");

How can I validate the Roles argument? I am using Fluent Assertion 1.7.1.1.

It is possible now in Fluent Assertion v2:

    typeof(SubscriberController).Should()
        .BeDecoratedWith<AuthorizeAttribute>(a => a.Roles.Contains("Admin"),
            "Subscriber controller users must be admins");
Jack Hughes
  • 5,514
  • 4
  • 27
  • 32

1 Answers1

2

Looks like that isn't possible now - Testing Arguments of Attributes

You can up this thread and ask about progress.

RredCat
  • 5,259
  • 5
  • 60
  • 100
  • Thanks for answering... pitty it isn't implemented yet, just validating that visitors are authorized isn't good enough unfortunately, don't want customers changing their own subscription. – Jack Hughes May 25 '12 at 10:38
  • 2
    We should be able to come up with a syntax for it. I'll create a item on the backlog for 2.0.0. – Dennis Doomen May 25 '12 at 11:10
  • Thanks Dennis, that would be great. It would fill quite a large hole in ASP.NET MVC automated testing that could only be overcome by resorting to full stack integration testing. – Jack Hughes May 25 '12 at 11:18
  • 1
    It's in the public beta of version 2.0 http://fluentassertions.codeplex.com/releases/view/82423 – Dennis Doomen Aug 25 '12 at 14:36