15

How to provide List as a data source for a data theory, I can't find anything in InlineData that supports this :

    [InlineData(null, new[] { 42, 2112 }, null)] // This doesn't work, I need something that works with List<int>
    [Trait("Category", "API")]
    [Trait("Category", "Partner")]
    [Trait("Category", "Smoke")]
    public void VerifyGetCarListAsync(int? colorID, List<int> carIDs, int? sellerID){//}
Ayoub Salhi
  • 312
  • 1
  • 4
  • 19

1 Answers1

26

This cannot be accomplished with InlineData you can only do this with MemberData, PropertyData or ClassData see the MemberData example below.

[MemberData(nameof(Data))]
public void VerifyGetCarListAsync(int? colorID, List<int> carIDs, int? sellerID){
    // test code
}


public static IEnumerable<object[]> Data(){
    yield return new object[] { null, new List<int>{ 42, 2112 }, null };
    yield return new object[] { 1, new List<int>{ 43, 2112 }, null };
    yield return new object[] { null, new List<int>{ 44, 2112 }, 6 };
}
behnam shateri
  • 1,223
  • 13
  • 18
Ids van der Zee
  • 814
  • 1
  • 13
  • 22
  • 3
    "=> {" does not compile – Michael Trullas Garcia Apr 07 '21 at 09:21
  • public static IEnumerable Data => new List { new object[] { 1, 2, 3 }, new object[] { -4, -6, -10 }, new object[] { -2, 2, 0 }, new object[] { int.MinValue, -1, int.MaxValue }, }; – ImZyzzBrah May 02 '21 at 15:42
  • 2
    You could do `[Theory] [InlineData(null, new[] { 42, 2112 }, null)] public void VerifyGetCarListAsync(int? colorID, int[] carIDs, int? sellerID) { var carIDList = carIDs.ToList(); }` – kalitsov Aug 23 '22 at 12:42
  • `PropertyData` has been renamed to `MemberData`. https://xunit.net/docs/upgrade-extensions – themefield Jun 29 '23 at 17:09