There doesn't seem to be a way to intercept/modify created values, but a solution I think I prefer is to use a second Fixture
.
public static IFixture FixDecimals(
this IFixture me
) {
var f = new Fixture();
var r = new Random();
me.Register<float>(() => f.Create<float>() + r.NextSingle());
me.Register<double>(() => f.Create<double>() + r.NextDouble());
me.Register<decimal>(() => f.Create<decimal>() + (decimal)r.NextDouble());
return me;
}
I think 'normally' you could use something like the following, but it doesn't work in these cases because these are value types.
// doesn't work
var r = new Random();
me.Customize<float>(f => f.Do(_f => _f += r.NextSingle()));
This doesn't work because Do
isn't a function but wants you to modify the 'instance', but since these are value types, modifying the 'instance' does nothing because we have a value not an object instance.