gpt4 book ai didi

c# - 使用 List、Autofixture 创建测试数据

转载 作者:太空宇宙 更新时间:2023-11-03 10:43:03 24 4
gpt4 key购买 nike

尝试让这个简单的测试工作:

public class MyClass
{
public string Text { get; set; }
public List<string> Comments { get; set; }

}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var fixture = new Fixture();
fixture.Customize<string>(c => c.FromSeed(s => s)); //Just return propertyname, no GUID appended
var test = fixture.Create<MyClass>();

}
}

但是我总是报错

The decorated ISpecimenBuilder could not create a specimen based on the request: System.String. This can happen if the request represents an interface or abstract class; if this is the case, register an ISpecimenBuilder that can create specimens based on the request. If this happens in a strongly typed Build<T> expression, try supplying a factory using one of the IFactoryComposer<T> methods.`

如果我删除 Customize线,似乎工作...

不太确定我需要做什么才能让它工作

最佳答案

您可以通过自定义 Fixture 实例来创建 string 实例,而无需附加 GUID,如下所示:

public void GuidsAreNotAppendedOnStringValues()
{
var fixture = new Fixture();
var expected = string.Empty;
fixture.Customizations.Add(
new StringGenerator(() => expected));

var actual = fixture.Create<MyClass>();

Assert.Equal(expected, actual.Comments.Aggregate((x, y) => x + y));
}

那样 Text 属性也是 Text,而不是 Texte85e2f6f-c1a3-47c7-baf2-4756c498f523

关于c# - 使用 List<string>、Autofixture 创建测试数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24573629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com