0

I'm having an issues unit testing the PermissionRequirementFilter below. This class was previously being DI'd into the unit test but something has changed and I'm unsure what's happened. It's complaining about the protection level of the class as it's an internal based class.

MainClass:

public class PermissionRequirementAttribute : TypeFilterAttribute
{
    public PermissionRequirementAttribute(string permissions) : base(typeof(PermissionRequirementFilter))
    {
        Arguments = new object[]
        {
            Regex.Split(permissions, @",\s*").ToList()
        };
    }
}

internal class PermissionRequirementFilter : IAuthorizationFilter
{
    private readonly IEnumerable<string> _requiredPermissions;
    private readonly IOrganisationsServiceProxy _organisationsProxy;

    public PermissionRequirementFilter(
        IOrganisationsServiceProxy organisationsProxy,
        IEnumerable<string> requiredPermissions)
    {
        _requiredPermissions = requiredPermissions;
        _organisationsProxy = organisationsProxy;
    }

    public void OnAuthorization(AuthorizationFilterContext context)
    {
    }
}

TestConstructor:

The test class looks like the code below but the import isn't letting me DI the class into the test class.

public class PermissionRequirementAttributeTests
{
    private readonly IEnumerable<string> _testPermissions;
    private readonly PermissionRequirementFilter _permissionRequirementFilter;
    private readonly Mock<IOrganisationsServiceProxy> _organisationProxy;

    public PermissionRequirementAttributeTests()
    {
        _organisationProxy = new Mock<IOrganisationsServiceProxy>();
        _testPermissions = new List<string> { "myPermission", "mySecondPermission", "myThirdPermission" };
        _permissionRequirementFilter = new PermissionRequirementFilter(_organisationProxy.Object, _testPermissions);
    }
}

What can I try to resolve this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Chris Marshall
  • 740
  • 1
  • 9
  • 25
  • 1
    `internal` perhaps ? – TheGeneral Nov 21 '19 at 22:46
  • 3
    An `internal` class isn't accessible to other assemblies, including your test project. You could use the [InternalsVisibleToAttribute](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.internalsvisibletoattribute?view=netframework-4.8) to mark the test project as being allowed to use internal types from your main project. – Jonathon Chase Nov 21 '19 at 22:48
  • It previously was an internal class which is the strange thing. Don't know if something else in the assembly has been removed as this was previously working. – Chris Marshall Nov 21 '19 at 22:48
  • 1
    Generally trying to achieve 100% class line coverage (including non-`public`) is problematic. Better to test against public contracts and test against `internal`s indirectly. That way you won't be bypassing intended design and behaviour. Otherwise your tests won't be just testing the system, but also anything you have introduced purely for the testing ecosystem - something that is irrelevant in PROD. Modifying the AUT environment just to suit unit tests leads to a false sense of confidence –  Nov 21 '19 at 22:49
  • 1
    The `InternalsVisibleToAttribute` attribute was probably removed from the SUT. Check source control history and see if someone removed it. – Kenneth K. Nov 21 '19 at 22:52
  • @MickyD I agree with what you have said but the problem I'm having is I need to set up the mock return value of a remote proxy call in the internal class. If that isnt setup then the whole attribute fails and cannot be tested. – Chris Marshall Nov 21 '19 at 23:22
  • There is no way for SO to figure out what happened with your code (and you should easily find out from source control history yourself). Overall guidance for testing internal classes is covered in duplicate. If you have some particular question on top of that - either ask new one or [edit] this one so it may be re-opened. – Alexei Levenkov Nov 22 '19 at 00:27

0 Answers0