带有 List 对象的 XUnit MemberData
XUnit MemberData with List objects
我正在编写一个 xunit 测试来测试根据空格拆分句子的实用方法的功能。例如:输入:"Who goes there?",输出:{"Who","goes","there"} collection/list 个字符串。
我尝试了以下
[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: We don't know how to serialize type System.Collections.Generic.List" 并且我理解这个问题。 Xunit 无法序列化列表。
鉴于我的用例,我该如何编写测试用例?
谢谢!
Xunit cannot serialize List.
诀窍是切换到数组。的确,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());
}
}
我正在编写一个 xunit 测试来测试根据空格拆分句子的实用方法的功能。例如:输入:"Who goes there?",输出:{"Who","goes","there"} collection/list 个字符串。
我尝试了以下
[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: We don't know how to serialize type System.Collections.Generic.List" 并且我理解这个问题。 Xunit 无法序列化列表。
鉴于我的用例,我该如何编写测试用例?
谢谢!
Xunit cannot serialize List.
诀窍是切换到数组。的确,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());
}
}