带有List对象的XUnit MemberData

时间:2017-06-23 22:22:27

标签: list parameters xunit testcase

我正在编写一个xunit测试来测试一个基于空格分割句子的实用程序方法的功能。例如:输入:“谁去那里?”,输出:{“Who”,“go”,“there”}字符串的集合/列表。

我尝试了以下

    [Theory]
    [MemberData(nameof(GetSplitWordHelperTestCases))]
    public void TestSplitWord(TestSplitWordHelper splitWordHelper)
    {
        var actualResult = (List<string>)splitWordHelper.Build().Item1.SplitWord();
        var expectedResult = (List<string>)splitWordHelper.Build().Item2;
        Assert.Equal(expectedResult.Count, actualResult.Count);

        for(int i = 0; i < actualResult.Count; i++)
        {
            Assert.Equal(expectedResult[i], actualResult[i]);
        }
    }

    public static IEnumerable<object[]> GetSplitWordHelperTestCases()
    {
        yield return new object[] { new TestSplitWordHelper("Hi There", 
new List<string> { "Hi", "There" }) };
    }

    public class TestSplitWordHelper : IXunitSerializable
    {
        private IEnumerable<string> results;
        private string source;

        public TestSplitWordHelper()
        {

        }

        public TestSplitWordHelper(string source, IEnumerable<string> results)
        {
            this.source = source;
            this.results = results;
        }

        public Tuple<string, IEnumerable<string>> Build()
        {
            return new Tuple<string, IEnumerable<string>>(source, this.results);
        }
        public void Deserialize(IXunitSerializationInfo info)
        {
            source = info.GetValue<string>("source");
            results = info.GetValue<IEnumerable<string>>("results");
        }

        public void Serialize(IXunitSerializationInfo info)
        {
            info.AddValue("source", source, typeof(string));
            info.AddValue("results", results, typeof(IEnumerable<string>));
        }

        public override string ToString()
        {
            return string.Join(" ", results.Select(x => x.ToString()).ToArray());
        }
    }

当我编译它时,我得到“System.ArgumentException:我们不知道如何序列化类型System.Collections.Generic.List”,我理解这个问题。 Xunit无法序列化List。

根据我的用例,我该如何编写测试用例?

谢谢!

1 个答案:

答案 0 :(得分:2)

  

Xunit无法序列化列表。

诀窍是切换到数组。的确,Xunit supports 这个。

这样将所有IEnumerable<string>List<string>切换为string[] ...和tada!它有效。

在经过修补和测试的代码版本下方(已应用的更改将附带注释)。

[Theory]
[MemberData(nameof(GetSplitWordHelperTestCases))]
public void TestSplitWord(TestSplitWordHelper splitWordHelper)
{
    var actualResult = splitWordHelper.Build().Item1.SplitWord().ToList();

    // (List<string>) changed to new List<string(...)
    var expectedResult = new List<string>(splitWordHelper.Build().Item2);
    Assert.Equal(expectedResult.Count, actualResult.Count);

    for (int i = 0; i < actualResult.Count; i++)
    {
        Assert.Equal(expectedResult[i], actualResult[i]);
    }
}

public static IEnumerable<object[]> GetSplitWordHelperTestCases()
{
    yield return new object[] { new TestSplitWordHelper("Hi There",

      // new List<string> changed to new string[]
      new string[] { "Hi", "There" }) };
}

public class TestSplitWordHelper : IXunitSerializable
{
    private string[] results;
    private string source;

    public TestSplitWordHelper()
    {

    }

    public TestSplitWordHelper(string source, string[] results)
    {
        this.source = source;
        this.results = results;
    }

    public Tuple<string, string[]> Build()
    {
        return new Tuple<string, string[]>(source, results);
    }
    public void Deserialize(IXunitSerializationInfo info)
    {
        source = info.GetValue<string>("source");

        // IEnumerable<string> changed to string[]
        results = info.GetValue<string[]>("results");
    }

    public void Serialize(IXunitSerializationInfo info)
    {
        info.AddValue("source", source, typeof(string));

        // IEnumerable<string> changed to string[]
        info.AddValue("results", results, typeof(string[]));
    }

    public override string ToString()
    {
        return string.Join(" ", results.Select(x => x.ToString()).ToArray());
    }
}