xUnit 理论指导作为参数 [英] xUnit theory guids as parametr

查看:25
本文介绍了xUnit 理论指导作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有测试字符串是否为 GUID 的扩展方法.

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);
}

我想通过 xUnit 和 Theory 对其进行测试.
对于字符串,它正在工作:

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);
}

但是我如何为一系列 Guid 做到这一点?我需要测试 Guid.Empty' 和 Guid.NewGuid`.

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

这不起作用:

[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();
    }
}

@编辑测试失败,因为

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()

推荐答案

您的测试方法目前只有一个参数 - 字符串 - 但并非必须如此.如果您的测试方法有 3 个参数怎么办?你是如何将它打包成 IEnumerable

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?>

因此,当您使用 xUnit 的属性数据"功能时,xUnit 要求该属性的形式为 IEnumerable

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() };
    }
}

当然,通过数据集传递预期结果"有点困难,这使得发明测试名称有点困难.您可以创建两个数据集:错误和良好,并制作两种测试方法,一种使用 Assert.False,另一种使用 Assert.True.. 但由于它是非常简单的测试,而且无论如何它都是大量数据驱动的,所以我喜欢写就这样.

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.

顺便说一下,这个例子还向你展示了为什么在 IEnumerable 中 object[] 而不仅仅是 string:可以有很多参数!

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

这篇关于xUnit 理论指导作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆