v2
v3
Warning
xUnit1026
"Theory methods should use all of their parameters"
Cause
This rule is triggered by having an unused parameter on your [Theory]
.
Reason for rule
Unused parameters are typically an indication of some kind of coding error (typically a parameter that was previously used and is no longer required, or a parameter that was overlooked in the test implementation).
Note
As of xunit.analyzers
version 1.2
, this rule will no longer trigger with parameters that are named as discards (meaning, starting with _
and optionally 1 or more numbers). For more information on discard parameter names, see IDE0060.
How to fix violations
To fix a violation of this rule, you may:
- Remove the unused parameter (and all associated data)
- Rename the parameter to a discard name
- Use the unused parameter
Examples
Violates
using Xunit;
static class MyGreetingService
{
public static string Greet(string name) =>
$"Hello, {name}!";
}
public class xUnit1026
{
[Theory]
[InlineData("Joe", 42)]
public void ValidateGreeting(string name, int age)
{
var result = MyGreetingService.Greet(name);
Assert.Equal("Hello, Joe!", result);
}
}
Does not violate
using Xunit;
static class MyGreetingService
{
public static string Greet(string name) =>
$"Hello, {name}!";
}
public class xUnit1026
{
[Theory]
[InlineData("Joe")]
public void ValidateGreeting(string name)
{
var result = MyGreetingService.Greet(name);
Assert.Equal("Hello, Joe!", result);
}
}
using Xunit;
static class MyGreetingService
{
public static string Greet(string name) =>
$"Hello, {name}!";
}
public class xUnit1026
{
[Theory]
[InlineData("Joe", 42)]
public void ValidateGreeting(string name, int _)
{
var result = MyGreetingService.Greet(name);
Assert.Equal("Hello, Joe!", result);
}
}
using Xunit;
static class MyGreetingService
{
public static string Greet(string name, int age) =>
$"Hello, {name}! How do you like being {age} years old?";
}
public class xUnit1026
{
[Theory]
[InlineData("Joe", 42)]
public void ValidateGreeting(string name, int age)
{
var result = MyGreetingService.Greet(name, age);
Assert.Equal("Hello, Joe! How do you like being 42 years old?", result);
}
}