Search code examples
c#tddautofixture

Autofixture - create a float, double or decimal with remainder


How do I modify AutoFixture create method for float, double and decimal, so that when these types get created they will also have a remainder?

Currently I do this, but this throws exception.

var fixture = new Fixture();
fixture.Customize<double>(sb => sb.FromFactory<double>(d => d * 1.33));   //This should add remainder
var value = fixture.Create<double>();

Solution

  • Attempting to redefine a type (double) by using a value of the same type (double) will, indeed, yield an infinite recursion. However, you can easily make this work by changing the seed input into another type - e.g. an int:

    var fixture = new Fixture();
    fixture.Customize<double>(c => c.FromFactory<int>(i => i * 1.33));
    var value = fixture.Create<double>();
    

    Doubles will now tend to have fractional values too.