Xunit 2.3.0 Unable to pass dates as inline params

Wijitha picture Wijitha · Dec 15, 2017 · Viewed 11.9k times · Source

In xUnit 2.2 and prior versions, we were able to pass date strings as inline data when implementing a Theory.

[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)
{

}

But with 2.3 update this seems to be broken and Visual studio is giving a compile error.

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

Has anyone got a workaround for this other that having to receive dates as strings and cast them inside the test method?

And would this be a temporary bug in this version and will be fixed in future version?

PS: I'm using xUnit on a .netcore project on VS2017

Answer

Ruben Bartelink picture Ruben Bartelink · Dec 15, 2017

You can make it explicit with 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)
{

}

(You can also make the property return IEnumerable<object[]>, which you'd typically do with yield return enumerator syntax, but I believe the above is the most legible syntax C# has to offer for it at present)