使用Autofixture Generator时,单元测试间歇性OOM异常

时间:2017-02-28 13:29:06

标签: c# nunit autofixture

我们有几个类似的单元测试,它们会抛出间歇性的OutOfMemoryExceptions并打破我们的CI管道:

public void Evaluate_Node1GreaterThanNode2_ReturnsTrue_Decimal()
{
    //Arrange
    var generator = _fixture.Create<Generator<decimal>>();

    var value1 = _fixture.Create<decimal>();
    var value2 = generator.Where(x => x < value1).First();
    Node.Node1.Evaluate(Arg.Any<IPeriod>(), Arg.Any<IColleaguePeriodDataManager>()).Returns(value1);
    Node.Node2.Evaluate(Arg.Any<IPeriod>(), Arg.Any<IColleaguePeriodDataManager>()).Returns(value2);

    //Act
    var result = Node.Evaluate();

    //Assert
    Assert.IsTrue(result);
    Assert.IsTrue(Node.EvaluatedResult);
}

正如您所看到的,我们正在使用AutoFixture的Generator来创建大于/小于初始值的数字:

generator.Where(x => x < value1).First();

显然这种方法不起作用。有人解决了吗?

这是错误和堆栈跟踪:

System.OutOfMemoryException : Array dimensions exceeded supported range.
at System.Collections.Generic.HashSet`1.SetCapacity(Int32 newSize, Boolean forceNewHashCodes)
   at System.Collections.Generic.HashSet`1.AddIfNotPresent(T value)
   at Ploeh.AutoFixture.RandomNumericSequenceGenerator.GetNextRandom()
   at Ploeh.AutoFixture.RandomNumericSequenceGenerator.CreateRandom(Type request)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TracingBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TracingBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.SpecimenFactory.Create[T](ISpecimenContext context, T seed)
   at Ploeh.AutoFixture.SpecimenFactory.Create[T](ISpecimenContext context)
   at Ploeh.AutoFixture.Generator`1.<GetEnumerator>d__2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)

1 个答案:

答案 0 :(得分:2)

看起来测试方案要求value2大于value1。一个简单的方法就是这样:

var x = fixture.Create<decimal>();
var y = fixture.Create<decimal>();
var value1 = Math.Min(x, y);
var value2 = Math.Max(x, y) + 0.0001m; // Add small fraction to make it strictly greater

如果您不想查看临时变量xy,可以将其重构为辅助方法。