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

标签 c# xunit xunit.net

在 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 给出了编译错误。

The value is not convertible to the method parameter 'startDate' of type 'System.DateTime

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

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

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

最佳答案

您可以使用 MemberDataAttribute 使其明确:-

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)}
};
      
[Theory, MemberData(nameof(CorrectData))]
public async Task WhenPassingCorrectData_SuccessfullyCreate(string title, 
                                             string description, 
                                             int categoryId, 
                                             string category, 
                                             DateTime startDate, 
                                             DateTime endDate)
{

}

(您还可以使属性返回 IEnumerable<object[]> ,通常使用 yield return enumerator syntax 执行此操作,但我相信上面是 C# 目前提供的最清晰的语法)

关于c# - Xunit 2.3.0 无法将日期作为内联参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47828374/

相关文章:

C#获取当前系统时间

c# - UWP:如何降低所选图像的质量?

ASP.NET MVC : testing a controller with XUnit

unit-testing - 如何在 xunit/autofixture 中组合 PropertyData 和 AutoNSubstituteData 属性?

unit-testing - 在 xUnit.net 中,是否可以按顺序运行测试?

unit-testing - 数据驱动的 xUnit 测试能否并行运行测试的所有实例(即针对每组数据)?

c# - 映射两个相同类型的对象(不包括某些字段)的最佳方法是什么?

c# - LinqToDB 如何将枚举存储为字符串值?

c# - 如何实现XUnit描述性Assert消息?

c# - .net 核心模拟 dbcontext 不工作(静态问题?)