.NET Core 2.2: xUnit Theory Inlinedata not working with enum values
Asked Answered
H

2

13

Does anybody know how to use xUnit with "Theory" and "InlineData" with enum values? This here is leading to the tests not being recognized as tests and not run:

[Theory]
[InlineData("12h", 12, PeriodUnit.Hour)]
[InlineData("3d", 3, PeriodUnit.Day)]
[InlineData("1m", 1, PeriodUnit.Month)]
public void ShouldParsePeriod(string periodString, int value, PeriodUnit periodUnit)
{
    var period = Period.Parse(periodString);
    period.Value.Should().Be(value);
    period.PeriodUnit.Should().Be(periodUnit);
}

The tests work and run if I use the int values of the enum instead of the enum values.

Hypogenous answered 25/5, 2019 at 16:54 Comment(0)
H
10

You don't need [MemberData], enum values should work right out of the box. As per the documentation enums are constants:

An enum type is a distinct value type (Value types) that declares a set of named constants.

Code example below works for me (a .net core 3.1 xUnit Test Project template):

public class UnitTest1
{
    public enum Foo { Bar, Baz, Qux }

    [Theory]
    [InlineData(Foo.Bar, Foo.Baz)]
    public void Test1(Foo left, Foo right)
    {
        Assert.NotEqual(left, right);
    }
}

Something else must be giving you troubles.

Hydrograph answered 13/10, 2019 at 15:4 Comment(6)
Since you accespted this answer, could you please elaborate on what was the real problem? That really interests me :)Beeck
@MikeLimaSierra: Mike, I am really very sorry, I saw your comment just right now and to be honest, I don't remember how this answer helped me at that time. I just saw that the tests are running fine, as of today, with "InlineData" and the Enum. Cheers, IngoHypogenous
"Something else must be giving you troubles." - I think there's an obscure bug in there. I'm trying to solve a case where test cases with enum values in InlineData won't run for some reason, either. I can copy the enum right on top of my test case (as shown in your minimal sample) and it will run, but when I remove that local enum definition again and use my actual enum (with the very same local name and constants!), the test case won't run.Heliopolis
Case in point: As soon as I move your Foo enum into another (referenced!) project (within the same solution as the test project), the test case stops working. In ReSharper's unit test sessions window, the test case will just disappear from the tree when I click "Run", and in the standard VS Test Explorer, the test case will just remain in its initial state of "Not Run", without any further explanation.Heliopolis
same issue here. In another file is unacceptableSpiccato
In case of having the enum in a referenced project, you need to pass the data as string and convert it to an enum value, see my answer here: https://mcmap.net/q/906542/-ienumerable-to-theorydataIssy
S
5

Attributes for [InlineData] need constant expressions, e.g int, bool, string etc.

Use [MemberData] instead if the inline is not recognizing the enum as a constant.

[Theory]
[MemberData(nameof(PeriodData))]
public void ShouldParsePeriod(string periodString, int value, PeriodUnit periodUnit) {
    var period = Period.Parse(periodString);
    period.Value.Should().Be(value);
    period.PeriodUnit.Should().Be(periodUnit);
}


public static IEnumerable<object[]> PeriodData() {
    yield return new object[] { "12h", 12, PeriodUnit.Hour };
    yield return new object[] { "3d", 3, PeriodUnit.Day };
    yield return new object[] { "1m", 1, PeriodUnit.Month };
}

Reference xUnit Theory: Working With InlineData, MemberData, ClassData

Swartz answered 12/10, 2019 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.