Null should only be used for nullable parameters
When you use [MemberData]
to target a method and pass that method arguments, the values must match
the nullability of the method parameters (that is, null
values are not allowed to be passed to
non-nullable parameters of the method data function).
Passing null
values to functions that aren’t expecting them can be cause for runtime errors.
To fix a violation of this rule, either replace the null
argument with a non-null
value, or
update the parameter to accept a nullable value.
using Xunit;
public class xUnit1034
{
public static TheoryData<string> TestData(int n, string s) =>
new() { $"Hello {s}! x {n}" };
[Theory]
[MemberData(nameof(TestData), 2112, null)]
public void TestMethod(string _)
{ }
}
using Xunit;
public class xUnit1034
{
public static TheoryData<string> TestData(int n, string s) =>
new() { $"Hello {s}! x {n}" };
[Theory]
[MemberData(nameof(TestData), 2112, "Brad")]
public void TestMethod(string _)
{ }
}
using Xunit;
public class xUnit1034
{
public static TheoryData<string> TestData(int n, string? s) =>
new() { $"Hello {s ?? "friend"}! x {n}" };
[Theory]
[MemberData(nameof(TestData), 2112, null)]
public void TestMethod(string _)
{ }
}