You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a semi-duplicate of #1538, but that issue was closed by the author because they thought the behaviour was a bug, and then realised that according to the docs it isn't. I'm creating this issue because I believe that it either is a bug, or the error message could stand to be more helpful.
Test case test class:
publicclassUnitTest1{privatestaticIEnumerable<object[]>GenerateTestData(){yieldreturnnewobject[]{newList<string>()};yieldreturnnewobject[]{default(List<string>)!};}// NOTE: not publicprivatestaticIEnumerable<object[]> TestData
=>new[]{newobject[]{newList<string>()},newobject[]{default(List<string>)!}};[Theory][MemberData(nameof(GenerateTestData))]publicvoidMemberData_Method(List<string>?data){
Assert.NotNull(data);}[Theory][MemberData(nameof(TestData))]publicvoidMemberData_Property(List<string>?data){
Assert.NotNull(data);}}
Since TestData is non-public, attempting to run the MemberData_Property test throws the aforementioned System.NotSupportedException : Specified method is not supported. That's it, nothing more, no stack trace, no indication as to the fact that you've made a mistake and need to fix your code.
First of all, why is this exception thrown? Why does the property need to be public when the method doing the exact same thing can have any visibility? The BCL's reflection API has no problems accessing a member of any type regardless of its visibility, so I don't understand the reason for this seemingly arbitrary restriction. If there is no good reason, this is a bug that should be fixed.
However, if there is a valid technical reason why non-public properties cannot be used as MemberData, then xUnit should not be throwing a raw NotSupportedException, because that's a terrible user experience. It should instead throw a specific exception with helpful text along the lines of "MemberData pointed to by {MemberName} is not accessible - if you're using a property, ensure it is public".
The text was updated successfully, but these errors were encountered:
On VSCode @ 1.70.0, with xunit @ 2.4.1 and xunit.runner.visualstudio @ 2.4.3
It seems this error is thrown in a few other conditions - such as when the method or property is not static; or the method or property does not return an IEnumerable<object[]>. The message is certainly lacking -- those requirements should probably be expressed.
Visual Studio 2022 version: 17.1.3
xunit NuGet package version: 2.4.1
xunit.runner.visualstudio NuGet package version: 2.4.3
This is a semi-duplicate of #1538, but that issue was closed by the author because they thought the behaviour was a bug, and then realised that according to the docs it isn't. I'm creating this issue because I believe that it either is a bug, or the error message could stand to be more helpful.
Test case test class:
Since
TestData
is non-public, attempting to run theMemberData_Property
test throws the aforementionedSystem.NotSupportedException : Specified method is not supported.
That's it, nothing more, no stack trace, no indication as to the fact that you've made a mistake and need to fix your code.First of all, why is this exception thrown? Why does the property need to be
public
when the method doing the exact same thing can have any visibility? The BCL's reflection API has no problems accessing a member of any type regardless of its visibility, so I don't understand the reason for this seemingly arbitrary restriction. If there is no good reason, this is a bug that should be fixed.However, if there is a valid technical reason why non-
public
properties cannot be used asMemberData
, then xUnit should not be throwing a rawNotSupportedException
, because that's a terrible user experience. It should instead throw a specific exception with helpful text along the lines of "MemberData pointed to by {MemberName} is not accessible - if you're using a property, ensure it is public".The text was updated successfully, but these errors were encountered: