Passing array of string to xunit validation method

I want to pass an array of strings to one of my XUnit methods, but when I just do the following, it doesn't work (array + params mechanism)

[Theory] [InlineData(new object[] { "2000-01-02", "2000-02-01" })] public void TestSynchronizeMissionStaffing_PeriodNoMatch(string[] dateStrings) 

I can work around the problem as follows:

  [Theory] [InlineData(0, new object[] { "2000-01-02", "2000-02-01" })] public void TestSynchronizeMissionStaffing_PeriodNoMatch(int dummy, string[] dateStrings) 

But I hope you can solve the problem.

Can you tell me?

+7
arrays xunit
source share
3 answers

This is a C # parameter function in which the array expands. therefore xunit cannot enter it in its one argument, you can force the array to force it, for example:

[InlineData((object)(new object[] { "2000-01-02", "2000-02-01" }))] see also here .

+5
source share

Use params before the method string[] argument, and then you do not need to initialize string[] in InlineData , rather you can use a variable number of string literals for which the compiler does not complain about one bit:

 [Theory] [InlineData("2000-01-02", "2000-02-01")] public void TestSynchronizeMissionStaffing_PeriodNoMatch(params string[] dateStrings) 
+3
source share

This should work

 [Theory] [InlineData(new object[] { new string[] { "2000-01-02", "2000-02-01" } })] public void TestSynchronizeMissionStaffing_PeriodNoMatch(string[] dateStrings) 

When u initializes an array of objects, as you did all the elements on it, it is one object, so when you try to pass a string array as a parameter, it passes the first element of the array of objects, which is "2000-01-02".

+1
source share

All Articles