AutoBogus icon indicating copy to clipboard operation
AutoBogus copied to clipboard

Generating a class with a .NET 6 `DateOnly` property fails

Open pdevito3 opened this issue 2 years ago • 10 comments

I have an entity that looks like this in a .NET 6 project:

public class Recipe
{
    public string Title { get; set; }

    public DateOnly Dob { get; set; } // context doesn't make sense, it's just for POC
}

When I run this faker:

public class FakeRecipe : AutoFaker<Recipe>
{
    public FakeRecipe()
    {
    }
}

in a test like this:

    [Test]
    public void test()
    {
        var test = new FakeRecipe().Generate();

        true.Should().BeTrue();
    }

I get this error.

System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> System.ArgumentOutOfRangeException : Year, Month, and Day parameters describe an un-representable DateTime.
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
   at AutoBogus.AutoBinder.CreateInstance[TType](AutoGenerateContext context)
   at AutoBogus.Generators.TypeGenerator`1.AutoBogus.IAutoGenerator.Generate(AutoGenerateContext context)
   at AutoBogus.AutoBinder.PopulateInstance[TType](Object instance, AutoGenerateContext context, IEnumerable`1 members)
   at AutoBogus.AutoFaker`1.<>c__DisplayClass30_0.<PrepareFinish>b__0(Faker faker, TType instance)
   at Bogus.Faker`1.PopulateInternal(T instance, String[] ruleSets)
   at Bogus.Faker`1.Generate(String ruleSets)
   at AutoBogus.AutoFaker`1.Generate(String ruleSets)
   at RecipeManagement.UnitTests.UnitTests.Wrappers.PagedListTests.test() in /RecipeManagement/tests/RecipeManagement.UnitTests/UnitTests/Wrappers/PagedListTests.cs:line 48
--ArgumentOutOfRangeException
   at System.DateOnly..ctor(Int32 year, Int32 month, Int32 day)

I know .NET 6 is new, but I have tons of use cases I'd like to refactor a DateOnly prop and this is a definite blocker with how my tests are currently set up :-(

pdevito3 avatar Nov 11 '21 04:11 pdevito3

Right after I submitted, I realized I should have checked Bogus... seems like they are tracking this here, but once it is implemented It will still need to be added to here in a new package reference?

pdevito3 avatar Nov 11 '21 04:11 pdevito3

@nickdodd79 FYI, Bogus just pushed v34.x which should accommodate this

pdevito3 avatar Nov 24 '21 15:11 pdevito3

Hey @pdevito3

Thanks for the update. I will get some updates in place so the latest Bogus and .NET capabilities are available.

Watch this space.

Nick.

nickdodd79 avatar Nov 24 '21 19:11 nickdodd79

Sounds good. No rush, just wanted to flag it in case you hadn't seen. Happy Turkey week!

pdevito3 avatar Nov 24 '21 19:11 pdevito3

hey @nickdodd79, what would the lift be for this? I can look into a PR myself if you're open to it and can give me a ballpark of what i should be looking at

pdevito3 avatar Feb 01 '22 03:02 pdevito3

thanks @pdevito3: we are currently using this lib (thanks @nickdodd79 !) and this would definitely solve our issue.

lsimone avatar May 10 '22 14:05 lsimone

@lsimone i ended up publishing a forked version of Autobogus given @nickdodd79's inactivity. It should fix this if you want to give it a whirl to resolve your issue. If you have any problems let me know: https://github.com/pdevito3/AutoBogus

Not sure how active I'll be maintaining it and really hoping nick takes things back over, but it's seeming more and more abandoned.

pdevito3 avatar Jul 02 '22 04:07 pdevito3

+1 for this fix, it's quite unfortunate. Thank you @pdevito3 for publishing the modified package. Just wondering, would using manual assembly binding redirects for Bogus also be an option?

TomasMorton avatar Jul 28 '22 21:07 TomasMorton

As a workaround you can use a CustomInstantiator on your class which sets the value like so:

public sealed class EntityFaker : AutoFaker<Entity>
{
    public EntityFaker()
    {
        CustomInstantiator(faker =>
            new Entity(DateOnly.FromDateTime(DateTime.UtcNow)));
    }
}

richardrobberse avatar Dec 11 '23 10:12 richardrobberse

@pdevito3 graciously implemented this in soenneker.utils.autobogus as well!

soenneker avatar Feb 20 '24 04:02 soenneker