MisterJames / GenFu

GenFu is a library you can use to generate realistic test data. It is composed of several property fillers that can populate commonly named properties through reflection using an internal database of values or randomly created data. You can override any of the fillers, give GenFu hints on how to fill them.
Other
831 stars 100 forks source link

Enum filler random issue #141

Closed CiprianCapus closed 4 years ago

CiprianCapus commented 6 years ago

Hi

It seems that enum fields are always filled with the same value I think the issue is that a new Random instance is created in EnumFiller.cs And for example in EnumerableExtensions.cs, a static Random field is used

public override object GetValue(object instance)
        {
            var values = Enum.GetValues(_type);

            return values.GetValue(**new Random()**.Next(values.Length-1));
        }
mfarkan commented 4 years ago

I think this issue was closed because the value of enum is generating randomly but if my enum have five items in it and length is five but i never get fifth item on this situation. I test it like that ;

public enum MyEnum
{
        a,
        b,
        c,
        d,
        e
}
for (int i = 0; i < 100; i++)
{
 A.Configure<MyClass>()
    .Fill(q => q.enummm)
    .Fill(q => q.Deneme)
    .Fill(q => q.Id);
  var instance = A.New<MyClass>();
  Console.WriteLine("my Enum value is :" + instance.enummm.ToString());
}

That's the result ;

my Enum value is :a
my Enum value is :b
my Enum value is :d
my Enum value is :b
my Enum value is :a
my Enum value is :d
my Enum value is :a
my Enum value is :a
my Enum value is :a
my Enum value is :a
my Enum value is :b
my Enum value is :d
my Enum value is :b
my Enum value is :d
my Enum value is :a
my Enum value is :d
my Enum value is :a
my Enum value is :b
my Enum value is :b
my Enum value is :a
my Enum value is :b
my Enum value is :b
my Enum value is :a
my Enum value is :a
my Enum value is :b
my Enum value is :d
my Enum value is :a
my Enum value is :c
my Enum value is :b
my Enum value is :c
my Enum value is :d
my Enum value is :a
my Enum value is :b
my Enum value is :a
my Enum value is :b
my Enum value is :d
my Enum value is :c
my Enum value is :c
my Enum value is :b
my Enum value is :d
my Enum value is :d
my Enum value is :a
my Enum value is :a
my Enum value is :c
my Enum value is :d
my Enum value is :c
my Enum value is :c
my Enum value is :d
my Enum value is :d
my Enum value is :d
my Enum value is :b
my Enum value is :a
my Enum value is :d
my Enum value is :a
my Enum value is :b
my Enum value is :b
my Enum value is :a
my Enum value is :b
my Enum value is :d
my Enum value is :d
my Enum value is :b
my Enum value is :a
my Enum value is :b
my Enum value is :a
my Enum value is :d
my Enum value is :d
my Enum value is :b
my Enum value is :d
my Enum value is :b
my Enum value is :a
my Enum value is :d
my Enum value is :a
my Enum value is :c
my Enum value is :a
my Enum value is :d
my Enum value is :d
my Enum value is :a
my Enum value is :a
my Enum value is :d
my Enum value is :b
my Enum value is :a
my Enum value is :d
my Enum value is :b
my Enum value is :b
my Enum value is :a
my Enum value is :c
my Enum value is :d
my Enum value is :d
my Enum value is :b
my Enum value is :c
my Enum value is :b
my Enum value is :a
my Enum value is :c
my Enum value is :b
my Enum value is :a
my Enum value is :b
my Enum value is :c
my Enum value is :c
my Enum value is :b
my Enum value is :d

What do you think @dpaquette ?

PS: .net Core 2.2 running on.

dpaquette commented 4 years ago

Yeah I could see that being an issue. I will take a look

mfarkan commented 4 years ago

I looked EnumFiller i saw this ;

public override object GetValue(object instance)
 {
   var values = Enum.GetValues(_type);
   return values.GetValue(new Random().Next(values.Length-1));
 }

I think main problem is subtraction.

dpaquette commented 4 years ago

Thanks for investigating @mfarkan and @CiprianCapus for reporting. This has been fixed and will be included in an upcoming release.