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' and
Guid.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()