mercredi 8 août 2018

Unit Test with current AppDomain as a parameter

I'm working on an asp.net mvc security module that uses some custom AuthorizeAttributes across my web project to control access to various resources.

I've got a separate project (Project.Security) that has various classes for implementing the security. One of these classes uses reflection to scan my web project (Project.Web) for all of my controllers and actions that are decorated with my custom Auth Attribute and build a list of all the found permissions.

I really haven't done much with unit testing, but would this be a good way for me to test out my code as I develop it? Right now the only way I know to test it is to debug the entire project. This can be time consuming since it might take a while to start up, then I'll have to stop the entire session to make some changes and start it back up again.

I understand reflection is something that happens at runtime, so without knowing a lot about unit testing I'm not really sure if this is possible or not. If there are any other approaches anybody could recommend I'd appreciate the advice.

Here's an example of the method I'm trying to test:

public static void ImportPermissions(this AppDomain appDomain)
    {
        var controllers = appDomain.GetAssemblies()
            .SelectMany(a => a.GetTypes())
            .Where(t => t != null
                        && t.IsPublic
                        && t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)
                        && !t.IsAbstract
                        && typeof(IController).IsAssignableFrom(t));





Aucun commentaire:

Enregistrer un commentaire