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
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