I have the following Unit test using XUnit:
[Theory]
[InlineData(1, 2, 3)]
[InlineData(-2, 2, 0)]
[InlineData(int.MinValue, -1, int.MaxValue)]
public void CanAddTheory(int value1, int value2, int expected) {
var calculator = new Calculator();
var result = calculator.Add(value1, value2);
Assert.Equal(expected, result);
}
public class Calculator {
public int Add(int value1, int value2) {
if (value1 == value2)
throw new ArgumentOutOfRangeException();
return value1 + value2;
}
}
Is there to use a Theory and also test if a method returns an exception?
In this example an exception would be returned if value1 == value2
:
[InlineData(2, 2, Exception???)]
[Fact]
or a[Theory]
with different parameters and useAssert.Throws()
as the test. – AlonInline
attribute accepts only constant values as a parameters. Exception is not. – Straitlaced