- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我尝试创建一个 SortedList<,>
使用 AutoFixture,但它会创建一个空列表:
var list = fixture.Create<SortedList<int, string>>();
我想出了以下生成项目的方法,但有点笨拙:
fixture.Register<SortedList<int, string>>(
() => new SortedList<int, string>(
fixture.CreateMany<KeyValuePair<int,string>>().ToDictionary(x => x.Key, x => x.Value)));
它不是通用的(强类型化为 int
和 string
)。我有两个不同的 TValue
SortedLists
创造。
有更好的建议吗?
最佳答案
这看起来像是 AutoFixture 应该开箱即用的功能,所以我添加了 an issue for that .
不过,在那之前,您可以执行以下操作。
首先,创建一个 ISpecimenBuilder
:
public class SortedListRelay : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
var t = request as Type;
if (t == null ||
!t.IsGenericType ||
t.GetGenericTypeDefinition() != typeof(SortedList<,>))
return new NoSpecimen();
var dictionaryType = typeof(IDictionary<,>)
.MakeGenericType(t.GetGenericArguments());
var dict = context.Resolve(dictionaryType);
return t
.GetConstructor(new[] { dictionaryType })
.Invoke(new[] { dict });
}
}
此实现只是概念验证。它在各个地方都缺乏适当的错误处理,但它应该演示该方法。它解决了一个 IDictionary<TKey, TValue>
来自 context
, 并使用返回值(已填充)创建 SortedList<TKey, TValue>
的实例.
为了使用它,您需要将它告诉 AutoFixture:
var fixture = new Fixture();
fixture.Customizations.Add(new SortedListRelay());
var actual = fixture.Create<SortedList<int, string>>();
Assert.NotEmpty(actual);
这个测试通过了。
关于c# - 如何使用 AutoFixture 创建 SortedList<Tkey, TValue>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37218084/
我刚刚更改了我的 [RegularExpression] 验证,我三分之一的单元测试失败了! 事实证明 AutoFixture 正在根据该正则表达式生成值,这很酷,但它不理解所有正则表达式,所以我想为
我使用 Autofixture 作为 SUT 工厂,但在卡住空实例时遇到困难。 我想做这样的事情: _fixture.Freeze(c => null); 但很快就意识到这是错误的。我已经用这个解决了
我想以不确定的方式自动生成方法的返回值,即每次调用/测试运行时,我都希望方法返回随机值。目前它总是返回方法调用的默认值: public interface IReturn {
给定这些类: public class DrumAndBassBand { public Drums Drum { get; set; } public Bass Bass { get
我正在将我的测试移植到 AutoFixture 2.0 ,而且我遇到了一些我既无法解释也无法解决的奇怪行为。这个简单的测试对我来说失败了: var autoFixtures = new Fixture
我正在研究一个相当嵌套的模型,它有一些循环引用。它还使用 Entity Framework ,因此所有列表都是 ICollection .为了适应这一点,我正在像这样配置 AutoFixture: _
我正在尝试使用 NSubstitute (1.8.2)、AutoFixture (3.33) 和 AutoFixture.AutoNSubstitute (3.33),如下所示: 我有一个 poco,
当我添加一个新的 TracingBehavior到自动夹具 Fixture实例使用 fixture.Behaviors.Add(new TracingBehavior()); 我在我的 R# 单元测试
我目前正在使用 EF Core Code First Approach,并且我有以下域类: public class Employee { // several properties such a
我刚刚开始使用 AutoFixture,我正在了解基础知识(从我所看到的还有更多),但我遇到了一个问题,我不能 100% 确定对于此类事情的最佳实践是什么。 我正在测试一个 Controller ,该
我在我的单元和集成测试中使用 AutoFixture 并遇到了问题。我正在生成数据传输对象,其中一些类在属性上具有 DataAnnotation 属性(其中一些是自定义的)。 AutoFixture
如何设置确定性测试以验证列表中的项目是否已排序? 首先,我执行以下操作: public void SyncListContainsSortedItems( [Frozen] SyncItem[
默认情况下,AutoFixture在“本地未指定时间”中创建DateTime结构。 我一直在尝试找到一种方法来配置它以创建具有UTC类型的DateTime结构,但是到目前为止没有成功。 有没有办法做到
是否可以在卡住后解冻对象类型? 如果我有一个使用 DateTime 的对象 Appointment,有没有办法做这样的事情? var time = fixture.Freeze(); IEnumera
有什么办法可以给 AutoFixture 一个对象的实例,让它通过所有的 setter 并设置随机数据? wiki 示例仅显示如何从 AutoFixture 获取实例,例如 var autoGener
在我使用 AutoFixture 之前的日子里,我可能做了以下安排来设置名为 CustomerService 的服务的单元测试: public void TestName() { //Arrang
我正在尝试编写这个简单的测试: var fixture = new Fixture().Customize(new AutoMoqCustomization()); var postProcessin
我正在尝试使用 SUT Factory “模式”来创建我的 SUT。 给定 SUT 结构: namespace MySut { public class Dep1 { }
我正在使用的域模型有很多循环引用。事实上,可以从图中的任何点到达大多数对象。许多这些循环引用也在集合中。所以一个 Booking将收藏 Students其中有 Courses 的集合其中有 Booki
我遇到了这个错误。 Ploeh.AutoFixture.Kernel.IllegalRequestException : A request for an IntPtr was detected. T
我是一名优秀的程序员,十分优秀!