gpt4 book ai didi

list - 带有 List 对象的 XUnit MemberData

转载 作者:行者123 更新时间:2023-12-02 04:05:10 25 4
gpt4 key购买 nike

我正在编写一个 xunit 测试来测试根据空格分割句子的实用方法的功能。例如:输入:“谁去那里?”,输出:{“Who”,“goes”,“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。

给定我的用例,如何编写测试用例?

谢谢!

最佳答案

Xunit cannot serialize List.

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

因此切换所有 IEnumerable<string>List<string>进入string[] ...还有田田!它有效。

下面是代码的修补和测试版本(已应用的更改用注释修饰)。

[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());
}
}

关于list - 带有 List 对象的 XUnit MemberData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44730693/

25 4 0