Paul Stanley
Paul Stanley

Reputation: 2161

XUnit Using a Dictionary As MemberData

I am having trouble with the syntax of how to get an XUnit Test Method to accept a dictionary as a paramater. This is my code which is broken:

Public static Dictionary<string, string> vals = new Dictionary<string, string> { { "a", "1" }, { "b", "2" }, { "ca", "3" } };
public static object[] dicList = new object[] { vals };

[Theory]
[MemberData(nameof(dicList))]

public void CheckSomething_ReturnsSomething(Dictionary<string, string> values)
{
 test stuff...

I am getting an error I understand from the error thrown that I need a return type of

IEnumerable<object[]>

I am unsure of how to achieve this with a dictionary.

Have added some code as suggested: I am still getting an error:

public static Dictionary<string, string> vals = new Dictionary<string, string> { { "a", "1" }, { "b", "2" }, { "ca", "3" } };
public static IEnumerable<object[]> dicList = new List<object[]> { new object[] { vals } };
[Theory]
[MemberData(dicList)] //error here 
    public void DriverLogon_CheckParams(Dictionary<string, string> values)
    {
    }

The new error is

error. "Cannot convert from IEnumerable<object[]> To string

Upvotes: 1

Views: 2713

Answers (1)

kor_
kor_

Reputation: 1530

The dicList must be an IEnumerable<object[]> like the error message says. This is because one item in the dicList contains all the parameter values for one test (this doesn't matter for your case since you only have one parameter, but it's good to know).

So your dicList becomes a List<object[]> instead of just plain object array:

public static Dictionary<string, string> vals = new Dictionary<string, string> { { "a", "1" }, { "b", "2" }, { "ca", "3" } };
public static IEnumerable<object[]> dicList = new List<object[]> { new object[] { vals } };

Upvotes: 2

Related Questions