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

See original GitHub issue

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 😦

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
pdevito3commented, 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.

1reaction
lsimonecommented, May 10, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem binding DateOnly in ASP.NET Core 6 MVC
NET 7. The workaround in the second issue is to create custom JsonConverter and ... the DateOnly property) then you have to use...
Read more >
DateOnly in .NET 6 and ASP.NET Core 6
Background. In the past, I used a derived class of Newtonsoft.Json.Converters.IsoDateTimeConverter for handling date only information. C#.
Read more >
How to use the DateOnly and TimeOnly structures
In this article ... The DateOnly and TimeOnly structures were introduced with .NET 6 and represent a specific date or time-of-day, respectively.
Read more >
Using DateOnly and TimeOnly in .NET 6 - Steve Gordon
DateOnly and TimeOnly allow developers to represent either the date or time portion of a DateTime. These two new types are structs (value...
Read more >
NET 6 DateOnly Property Not Deserialized Correctly #2908
It seems it's unable to deserialize this property correctly when fetching. To Reproduce. public class TestClass { public string Id { get; set;...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found