Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying the return value for xUnit Theory tests

Is it possible to specify the return value for a theory case? Like I have the test methode:

[Theory]
[MemberData(nameof(TestCases))]
public async Task<bool> Test(int i, double d, string str)
{
    return DoStuffAsync(i, d, str);
}

and the methode

public static IEnumerable<object[]> TestCases()
{
    yield return new object[] { 1, Math.PI, "case1", true };
    yield return new object[] { 2, Math.E,  "case2", false };
}

to produce the test cases.

But if I try to run these test cases, I get the error

System.InvalidOperationException : The test method expected 3 parameter values, but 4 parameter values were provided.
like image 332
Ackdari Avatar asked Sep 15 '25 06:09

Ackdari


1 Answers

You can rewrite your test a little bit, by adding the parameter for expected result and compare it in Assert with value, returned from DoStuffAsync method. Also don't forget to change the return value for test method from Task<bool> to just Task or async void, since you are checking the return value inside test and return nothing

[Theory]
[MemberData(nameof(TestCases))]
public async Task Test(int i, double d, string str, bool expected)
{
    var result = await DoStuffAsync(i, d, str);
    Assert.Equal(expected, result);
}
like image 115
Pavel Anikhouski Avatar answered Sep 17 '25 19:09

Pavel Anikhouski