开发者

How to pass string arrays to Theory tests using PropertyData

I have a test method with the following signature:

[Theory]
[PropertyData("MyTestData")]
public void ProcessLines_validLines_doStuff(string[] lines)
{
   // do stuff
}

My property looks like so:

public static IEnumerable&l开发者_如何学Ct;string[]> MyTestData
{
get
{
 List<string[]> data = new List<string[]>
 {
    new[] { "1", "1"},
    new[] { "2", "2"}
 };

 var iter = data.GetEnumerator();

 while (iter.MoveNext())
 yeld return iter.Current;
}
}

Xunit throws a System.InvalidOperation: Expected 1 parameter, got 2 parameters

Any ideas?


The properties type should be IEnumerable<object[]>. The object array corresponds to the list of parameters. Even if your test method has only one parameter you need to return an array, even though it has only one element.

In your case you could specify the type as IEnumerable<string[][]> but that was probably what caused the confusion.

public static IEnumerable<object[]> MyTestData
{
    get
    {
        var data = new []
        {
            new[] { new []{ "1", "1"}},
            new[] { new []{ "2", "2"}}
        };

        return data;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜