NUnit1023
The target method expects parameters which cannot be supplied by the ValueSource
Topic | Value |
---|---|
Id | NUnit1023 |
Severity | Error |
Enabled | True |
Category | Structure |
Code | ValueSourceUsageAnalyzer |
Description
The target method expects parameters which cannot be supplied by the ValueSource.
Motivation
To prevent tests that will fail at runtime due to improper construction.
How to fix violations
Example Violation
public class MyTestClass
{
[Test]
public void StringTest([ValueSource(nameof(Strings))] string input)
{
Assert.That(input, Is.Not.Null);
}
static IEnumerable<string> Strings(string first, string second)
{
yield return first;
yield return second;
}
}
Explanation
In the sample above, the method Strings
expects two arguments, but the ValueSource
cannot supply arguments.
Fix
Change Strings
so that it does not expect any arguments:
public class MyTestClass
{
[Test]
public void StringTest([ValueSource(nameof(Strings))] string input)
{
Assert.That(input, Is.Not.Null);
}
static IEnumerable<string> Strings()
{
yield return "first";
yield return "second";
}
}
Configure severity
Via ruleset file
Configure the severity per project, for more info see MSDN.
Via .editorconfig file
# NUnit1023: The target method expects parameters which cannot be supplied by the ValueSource
dotnet_diagnostic.NUnit1023.severity = chosenSeverity
where chosenSeverity
can be one of none
, silent
, suggestion
, warning
, or error
.
Via #pragma directive
#pragma warning disable NUnit1023 // The target method expects parameters which cannot be supplied by the ValueSource
Code violating the rule here
#pragma warning restore NUnit1023 // The target method expects parameters which cannot be supplied by the ValueSource
Or put this at the top of the file to disable all instances.
#pragma warning disable NUnit1023 // The target method expects parameters which cannot be supplied by the ValueSource
Via attribute [SuppressMessage]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Structure",
"NUnit1023:The target method expects parameters which cannot be supplied by the ValueSource",
Justification = "Reason...")]