Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating a class with a .NET 6 DateOnly property fails #76

Open
pdevito3 opened this issue Nov 11, 2021 · 10 comments · May be fixed by #84
Open

Generating a class with a .NET 6 DateOnly property fails #76

pdevito3 opened this issue Nov 11, 2021 · 10 comments · May be fixed by #84

Comments

@pdevito3
Copy link

pdevito3 commented Nov 11, 2021

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
Copy link
Author

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
Copy link
Author

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

@nickdodd79
Copy link
Owner

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.

@pdevito3
Copy link
Author

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

@pdevito3
Copy link
Author

pdevito3 commented Feb 1, 2022

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 pdevito3 linked a pull request Apr 30, 2022 that will close this issue
@lsimone
Copy link

lsimone commented May 10, 2022

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

@pdevito3
Copy link
Author

pdevito3 commented Jul 2, 2022

@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.

@TomasMorton
Copy link

+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?

@richardrobberse
Copy link

richardrobberse commented Dec 11, 2023

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)));
    }
}

@soenneker
Copy link

soenneker commented Feb 20, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants