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?
internalperhaps ?internalclass 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.public) is problematic. Better to test against public contracts and test againstinternals 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 confidenceInternalsVisibleToAttributeattribute was probably removed from the SUT. Check source control history and see if someone removed it.