AutoBogus
AutoBogus copied to clipboard
Generating a class with a .NET 6 `DateOnly` property fails
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 :-(
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?
@nickdodd79 FYI, Bogus just pushed v34.x which should accommodate this
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.
Sounds good. No rush, just wanted to flag it in case you hadn't seen. Happy Turkey week!
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
thanks @pdevito3: we are currently using this lib (thanks @nickdodd79 !) and this would definitely solve our issue.
@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.
+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?
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)));
}
}
@pdevito3 graciously implemented this in soenneker.utils.autobogus as well!