AutoBogus icon indicating copy to clipboard operation
AutoBogus copied to clipboard

Non-Generic Generate support

Open gkruszewski opened this issue 2 years ago • 8 comments

Can we add static non-generic counterparts for the Generate methods that accept a Type to the AutoFaker class?

gkruszewski avatar Nov 04 '21 04:11 gkruszewski

Hey @gkruszewski

Shouldn't be difficult to add non-generic support. I will see what I can do.

Nick.

nickdodd79 avatar Nov 24 '21 19:11 nickdodd79

This is the basic approach I've been playing around with:

var client1 = new AutoFaker<Client>().Generate();

is equivalent to:

var fakerType = typeof(AutoFaker<>).MakeGenericType(new[]{typeof(Client)});
var faker = Activator.CreateInstance(fakerType);
var client1 = fakerType.GetMethod("Generate", new[] { typeof(string) }).Invoke(faker, new object?[] { null});

pwhe23 avatar Mar 17 '22 20:03 pwhe23

I was about to open the same issue. Please expose such API, though MakeGenericMethod approach works, it's better to have it without using reflection.

jvmlet avatar Feb 13 '23 19:02 jvmlet

@nickdodd79 , would you accept such PR to AutoGenerateContextExtensions ?

public static object Generate(this AutoGenerateContext context,Type typeToGenerate)
    {
      if (context != null)
      {
        // Set the generate type for the current request
        context.GenerateType =typeToGenerate;

        // Get the type generator and return a value
        var generator = AutoGeneratorFactory.GetGenerator(context);
        return generator.Generate(context);
      }

      return default;
    }

jvmlet avatar Mar 07 '23 14:03 jvmlet

+1 for this. I need this for unit testing where you have a [Theory]

relfman-cmg avatar Mar 30 '23 23:03 relfman-cmg

This is a very, very interesting feature request ! May I ask what's the status :) ?

NinjaCross avatar Feb 02 '24 18:02 NinjaCross

@NinjaCross I think this project is abandoned.

Not to worry though!

I recently added this non-type parameter feature to soenneker.utils.autobogus

soenneker avatar Feb 12 '24 02:02 soenneker

Thankyou @soenneker !

NinjaCross avatar Feb 12 '24 11:02 NinjaCross