Xunit 2.3.0 无法将日期作为内联参数传递

问题描述 投票:0回答:4

在 xUnit 2.2 及之前的版本中,我们能够在实现理论时将日期字符串作为内联数据传递。

[Theory]
[InlineData("title 1", "testing 1", 1, "Educational", "2017-3-1", "2018-12-31")]
[InlineData("title 2", "testing 2", 2, "Self Employment", "2017-2-1", "2018-2-28")]
public async Task WhenPassingCorrectData_SuccessfullyCreate(
    string title,
    string description,
    int categoryId,
    string category,
    DateTime startDate,
    DateTime endDate)
{

}

但是随着 2.3 更新,这似乎被破坏了,Visual studio 给出了编译错误。

该值无法转换为方法参数“startDate” 输入“系统.日期时间

有没有人有解决方法来解决这个问题,即必须以字符串形式接收日期并将它们投射到测试方法中?

这是否是此版本中的临时错误,并将在未来版本中修复?

PS:我在 VS2017 上的 .netcore 项目上使用 xUnit

c# xunit xunit.net
4个回答
70
投票

您可以使用

MemberDataAttribute
C# 12 集合表达式 使其明确:-

// NB C# 12 Collection Expressions - see below for backcompat version if not building with v8 SDK
public static readonly object[][] CorrectData =
[
    ["title 1", "testing 1", 1, "Educational", new DateTime(2017, 3, 1), new DateTime(2018, 12, 31)],
    ["title 2", "testing 2", 2, "Self Employment", new DateTime(2017, 2, 1), new DateTime(2018, 2, 28)]
];      

[Theory, MemberData(nameof(CorrectData))]
public async Task WhenPassingCorrectData_SuccessfullyCreate(string title, 
                                             string description, 
                                             int categoryId, 
                                             string category, 
                                             DateTime startDate, 
                                             DateTime endDate)
{
}

如果您没有使用 .NET 8 SDK,那么

CorrectData
的最佳语法很遗憾:

public static readonly object[][] CorrectData =
{
  new object[] { "title 1", "testing 1", 1, "Educational", 
                  new DateTime(2017,3,1), new DateTime(2018,12,31)},
  new object[] { "title 2", "testing 2", 2, "Self Employment", 
                  new DateTime(2017, 2, 1), new DateTime(2018, 2, 28)}
};

(您还可以使属性返回

IEnumerable<object[]>
,通常使用
yield return
枚举器语法
来实现,但我相信以上是 C# 目前提供的最清晰的语法)


19
投票

目前更好的方法是使用 TheoryData,以便您可以使用强类型输入。 使用 TheoryData 创建强类型 xUnit 理论测试数据

TheoryData<DateTime> MemberData = new TheoryData<DateTime>
{
 DateTime.Now, 
 new DateTime(), 
 DateTime.Max
}

10
投票

这个bug似乎在v2.4.0+中得到了修复(这个功能至少在v2.3.1中停止工作了?)

如果无法将 xunit 升级到具有修复程序的版本,那么也许可以执行与 xunit 隐式执行的操作相同的操作:

https://github.com/xunit/xunit/blob/07457dab8d0bb188e74e476c062a4a9aeca44711/src/xunit.v3.common/Reflection/Reflector.cs#L88-L92

[Theory]
[InlineData("title 1", "testing 1", 1, "Educational", "2017-3-1", "2018-12-31")]
[InlineData("title 2", "testing 2", 2, "Self Employment", "2017-2-1", "2018-2-28")]
public async Task WhenPassingCorrectData_SuccessfullyCreate(
    string title,
    string description,
    int categoryId,
    string category,
    string startDate, // <-- change from `DateTime` to `string`
    string endDate)   // <-- change from `DateTime` to `string`
{
    var expectedStartDate = DateTime.Parse(startDate); // <-- add this
    var expectedEndDate = DateTime.Parse(endDate);     // <-- add this

    //  rest of test ...

}

否则,如果有更复杂的测试,那么也许可以像其他人建议的那样使用

MemberDataAttribute


5
投票

这是将强类型测试数据传递到 xUnit Tests 的好方法

博客文章

源代码

public class SampleData
{
    public int A { get; set; }
    public int B { get; set; }
    public int C => A + B;
}

public class UnitTest1
{
    /// <summary>
    /// The test data must have this return type and should be static
    /// </summary>
    public static IEnumerable<object[]> TestData
    {
        get
        {
            //Load the sample data from some source like JSON or CSV here.
            var sampleDataList = new List<SampleData>
            {
                new SampleData { A = 1, B = 2 },
                new SampleData { A = 3, B = 2 },
                new SampleData { A = 2, B = 2 },
                new SampleData { A = 3, B = 23 },
                new SampleData { A = 43, B = 2 },
                new SampleData { A = 3, B = 22 },
                new SampleData { A = 8, B = 2 },
                new SampleData { A = 7, B = 25 },
                new SampleData { A = 6, B = 27 },
                new SampleData { A = 5, B = 2 }
         };

            var retVal = new List<object[]>();
            foreach(var sampleData in sampleDataList)
            {
                //Add the strongly typed data to an array of objects with one element. This is what xUnit expects.
                retVal.Add(new object[] { sampleData });
            }
            return retVal;
        }
    }

/* Alternate form

    public static IEnumerable<object[]> TestData() 
    {
        yield return new [] { new SampleData { A = 1, B = 2 } };
        yield return new [] { new SampleData { A = 3, B = 2 } };
        yield return new [] { new SampleData { A = 2, B = 2 } };
        yield return new [] { new SampleData { A = 3, B = 23 } };
        yield return new [] { new SampleData { A = 43, B = 2 } };
        yield return new [] { new SampleData { A = 3, B = 22 } };
        yield return new [] { new SampleData { A = 8, B = 2 } };
        yield return new [] { new SampleData { A = 7, B = 25 } };
        yield return new [] { new SampleData { A = 6, B = 27 } };
        yield return new [] { new SampleData { A = 5, B = 2 } };
    }
*/

/* Or:

    public static IEnumerable<object[]> TestData() =>
        from x in new[] { 
            new SampleData { A = 1, B = 2 },
            new SampleData { A = 3, B = 2 },
            new SampleData { A = 2, B = 2 },
            new SampleData { A = 3, B = 23 },
            new SampleData { A = 43, B = 2 },
            new SampleData { A = 3, B = 22 },
            new SampleData { A = 8, B = 2 },
            new SampleData { A = 7, B = 25 },
            new SampleData { A = 6, B = 27 },
            new SampleData { A = 5, B = 2 } }
        select new object[] { x};
    */

    /// <summary>
    /// Specify the test data property with an attribute. This method will get executed 
    /// for each SampleData object in the list
    /// </summary>
    [Theory, MemberData(nameof(TestData))]       
    public void Test1(SampleData sampleData)
    {
        Assert.Equal(sampleData.A + sampleData.B, sampleData.C);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.