-
-
Notifications
You must be signed in to change notification settings - Fork 478
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
RuleForType on object graphs #188
Comments
Hi Steve, Thanks for your issue. I appreciate it. Ultimately, though, you'll have to raise the issue with Nick over at AutoBogus if you think I can only speak for Bogus and how
In other words,
Instead you can write:
Additionally, Bogus won't fill a collection of type If you have deeply nested objects or graphs of type For better or worse, Bogus is pretty dumb and mechanical, it wont do anything unless it is told to. 😄 So then the question we're all wondering is, how do you make your third situation work? One way, manually: private static void Manually(){
var moneyFaker = new AutoFaker<Money>()
.RuleFor(x => x.DecimalValue, f => f.Finance.Amount() );
var childMoneyFaker = new AutoFaker<ChildWithMoney>()
.RuleFor( x => x.Amount, f => moneyFaker.Generate());
var parentWithMoneyFaker = new Faker<ParentWithMoney>()
.RuleFor( x => x.Amount, f => moneyFaker.Generate() )
.RuleFor( x => x.Children, f => childMoneyFaker.Generate(3) );
parentWithMoneyFaker.Generate();
} Sometimes, manual work is no fun. I'm lazy, I want the computer to do it for me. What if we want to have "registration" like behavior when some type So, getting more crazy, you'll probably need to create your own binder: private static void GetLazyCrazy()
{
var parentWithMoneyFaker = new AutoFaker<ParentWithMoney>(new CustomBinder());
var fake = parentWithMoneyFaker.Generate();
fake.Dump();
}
public class CustomBinder : AutoBinder
{
public override TType CreateInstance<TType>(AutoGenerateContext context)
{
if (typeof(TType) == typeof(Money)) return (TType)MakeMoney(); //wish it was this easy
return base.CreateInstance<TType>(context);
}
public override void PopulateInstance<TType>(object instance, AutoGenerateContext context, IEnumerable<MemberInfo> members = null)
{
if (typeof(TType) == typeof(Money)) return; //already populated from create instance.
base.PopulateInstance<TType>(instance, context, members);
}
public static object MakeMoney() => //the life, ay?
new AutoFaker<Money>()
.RuleFor(x => x.DecimalValue, f => f.Finance.Amount())
.Generate();
} (you could probably generalize this into some kind of registration object for your application to make it extensible) Hopefully my answer is clear and gives you a way out of your predicament. Perhaps (and probably) Nick has a few better ideas. I do wish using Bogus to automagically create object graphs was much easier, and that's what #8 and #74 are all about; but the actual implementation and problem is a difficult one. Some day we might have the situation covered, but for now, this is how you'll have to approach the problem AFAIK. If you have any more questions, please feel free to continue to ask. Thanks, 🤔 ✨ "Do you ponder the manner of things... yeah yeah... like glitter and gold..." |
Thanks for such an outstanding answer. In the meantime I've done more or less what you've suggested. I'd like to submit a PR to crystallize your response in the docs - that cool? |
Hi Steve, Sure, sounds fine. Send in a PR when you're ready. 😸 Thanks, |
Thanks for this awesome library. I've noticed that
RuleForType
does not appear to work on child objects.We're using the
Google.Api.CommonProtos
library in our app, and theMoney
type in this package has aDecimalValue
field that allows setting the amount as a native .NET Decimal, but it only supports 9 digits after the decimal point, and it will throw on creation if theDecimalValue
is set to a value that isn't supported. This works great at the root level via a RuleForType like this:.RuleForType(typeof(Money), f => new Money { DecimalValue = f.Finance.Amount() })
However, this same RuleForType is not used when generating the
Money
values for children.Is this a bug? I would have expected RuleForType to work for any child objects generated by the AutoFaker.
Demo console app below. You will have to install
AutoBogus
andGoogle.Api.CommonProtos
NuGet packages.The text was updated successfully, but these errors were encountered: