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?

7
  • 1
    internal perhaps ? Commented Nov 21, 2019 at 22:46
  • 3
    An internal class isn't accessible to other assemblies, including your test project. You could use the InternalsVisibleToAttribute to mark the test project as being allowed to use internal types from your main project. Commented Nov 21, 2019 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. Commented Nov 21, 2019 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 internals 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 Commented Nov 21, 2019 at 22:49
  • 1
    The InternalsVisibleToAttribute attribute was probably removed from the SUT. Check source control history and see if someone removed it. Commented Nov 21, 2019 at 22:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.