在 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/