I have internal enum MyEnum
in the project.
And I also have a custom attribute on some of the values of MyEnum
, like:
internal enum MyEnum
{
[CustomAttribute("value")]
EnumValue,
}
My project also contains class InternalsVisibleTo.cs
:
[assembly: InternalsVisibleTo("MyTestProjectName")]
And I'd like to cover this attribute in xUnit Theory, i.e.:
[Theory]
[InlineData(MyEnum.EnumValue, "value")]
public void MyAttributeWorksCorrectly(MyEnum myEnum, string expectedAttributeValue)
{
}
Because InternalsVisibleTo - InlineData has access to the enum. However, I get the following error:
Inconsistent accessibility: parameter type 'MyTestProjectName.MyEnum' is less accessible than method 'MyAttributeWorksCorrectly'
Sure - this is violation.
Question - can I bypass it somehow without makine MyEnum public
?
I've already read few articles:
https://learn.microsoft.com/en-us/dotnet/standard/assembly/friend https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/accessibility-levels
That is because you method
MyAttributeWorksCorrectly
ispublic
, that means that anybody could call this method. ButMyEnum
isinternal
which means only the current assembly (and any assembly that you define withInternalsVisibleTo
-attribute) has access to this Type.Think about that, what if your test assembly gets used by somebody else, and they want to call the Method
MyAttributeWorksCorrectly
, they would also know of the existence ofMyEnum
which they should not because of theinternal
-modifier. That is why the compiler does not let you compile.You can fix this Issue by simply marking the UniTest as
internal
or by using a[Fact]
-UnitTest, of course there you do not have the possibility with dynamic data.