Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xUnit theory guids as parametr

Tags:

c#

xunit

I have extension method which tests if string is GUID.

public static bool IsGuid(this string str)
{
    if(str == null)
        throw new ArgumentNullException(str, "Argument can not be NULL");
    Guid guidFromString;
    return Guid.TryParse(str, out guidFromString);
}

I want test it via xUnit and Theory.
For string it's working:

[Theory, InlineData(""), InlineData(" ")]
public void IsGuid_EmptyOrWhiteSpace_ShouldReturnFalse(string str)
{
    // Arrange
    bool result;

    // Act
    result = str.IsGuid();

    // Assert
    Assert.False(result);
}

But how I can do it for array of Guids? I need test Guid.Empty' andGuid.NewGuid`.

This not work:

[Theory, MemberData(nameof(Guids))]
public void IsGuid_EmptyOrValidGuid_ShouldReturnTrue(string str)
{
    // Arrange
    bool result;

    // Act
    result = str.IsGuid();

    // Assert
    Assert.False(result);
}

public static IEnumerable<string> Guids
{
    get
    {
        yield return Guid.Empty.ToString();
        yield return Guid.NewGuid().ToString();
    }
}

@Edit Test fails because

System.ArgumentException
Property Guids on ExtensionsLibraryTests.StringExtensions.xUnitStringExtensionsTests yielded an item that is not an object[]
   at Xunit.MemberDataAttribute.ConvertDataItem(MethodInfo testMethod, Object item)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Xunit.Sdk.XunitTheoryTestCaseRunner.<AfterTestCaseStartingAsync>d__7.MoveNext()
like image 800
Nerf Avatar asked Oct 29 '25 08:10

Nerf


1 Answers

Your test method has currently just one argument - string - but that does not has to be the case. What if your test method had 3 arguments? How'd you pack it into a IEnumerable<what-here?>

For this reason, when you use 'property data' feature of xUnit, xUnit requires that property to be in form of IEnumerable<object[]>

public static IEnumerable<object[]> Guids
{
    get
    {
        yield return new object[]{ "" };
        yield return new object[]{ " " };
    }
}

That should solve the immediate problem. However, I'd encourage you to try out this layout:

[Theory, MemberData(nameof(Guids))]
public void thinkofsomesmartname(bool expectedResult, string text)
{
    bool result = text.IsGuid();

    Assert.Equal(expectedResult, result);
}

public static IEnumerable<object[]> Guids
{
    get
    {
        yield return new object[]{ false, "" };
        yield return new object[]{ false, " " };
        yield return new object[]{ true, Guid.NewGuid().ToString() };
    }
}

Of course, it's a bit of hack to pass the 'expected result' through the data set, and that makes inventing the test name a bit hard. You could create two data sets: wrong and good, and make two test methods one with Assert.False, and one with Assert.True.. but since it's very simple test and since it is heavily data-driven anyways, I like to write it that way.

By the way, this example also shows you why object[] and not just string in the IEnumerable: can have many parameters!

like image 142
quetzalcoatl Avatar answered Oct 30 '25 23:10

quetzalcoatl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!