AutoFixture oluşturma yöntemini float, double ve decimal için nasıl değiştirebilirim, böylece bu türler oluşturulduğunda kalanlar da olur mu?Otomatik Düzeltme - kalanı ile bir kayan nokta, ikili veya ondalık oluşturmak
Şu anda bunu yapıyorum, ancak bu istisna atar.
var fixture = new Fixture();
fixture.Customizations.Add(
new RandomDoublePrecisionFloatingPointSequenceGenerator());
RandomDoublePrecisionFloatingPointSequenceGenerator
aşağıda gibi görünebilir::
internal class RandomDoublePrecisionFloatingPointSequenceGenerator
: ISpecimenBuilder
{
private readonly object syncRoot;
private readonly Random random;
internal RandomDoublePrecisionFloatingPointSequenceGenerator()
{
this.syncRoot = new object();
this.random = new Random();
}
public object Create(object request, ISpecimenContext context)
{
var type = request as Type;
if (type == null)
return new NoSpecimen(request);
return this.CreateRandom(type);
}
private double GetNextRandom()
{
lock (this.syncRoot)
{
return this.random.NextDouble();
}
}
private object CreateRandom(Type request)
{
switch (Type.GetTypeCode(request))
{
case TypeCode.Decimal:
return (decimal)
this.GetNextRandom();
case TypeCode.Double:
return (double)
this.GetNextRandom();
case TypeCode.Single:
return (float)
this.GetNextRandom();
default:
return new NoSpecimen(request);
}
}
}
Sanırım arka planda işlerin nasıl yürüdüğünü ve özelleştirmenin sonsuz bir özyineleme yarattığını düşünmedim. Bunu açıkladığın için teşekkürler! – Rok
+1 Bu oldukça düzgün! :) –