You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The typed Faker class is great to create complex builder scenarios, the power of defining the shape of the type in parts as necessary is one of the best things to solve problems like test scenarios.
Things get a bit complicated when the Type has a complex constructor or a mix with required properties:
varbuilder=newFaker<User>().RuleFor(x => x.Age,f => f.Random.Int(18,50)).RuleFor(x => x.Name,f => f.Person.FirstName).CustomInstantiator(f =>new User(f.Person.Email){Name=// need to be set //,Age=// need to be set //,});
In this case, we kinda lost the power of member composability. If it was a custom builder I would have to create fields to hold values for the CustomInstantiator etc.
So would be nice to have a way to get the shape of the data without constructing the actual Type
A generic way to solve this could be to have a new method like GetJsonObject, which for the case above could be something like:
voidMain(){
User GenerateUser(){varf=new Faker();varuser=new User(f.Person.Email){Name= f.Person.FirstName,Age= f.Random.Int(18,50)};returnuser;}
GenerateUser().Dump();}publicclassUser{publicUser(stringemail)=>Email=email;publicstringName{get;init;}publicintAge{get;init;}publicstringEmail{get;init;}}
As you've indicated, Faker<T> has some limits before object design complexity gets in our way; and in such cases, we should consider a non-typed Faker as a suitable and reasonable alternative.
The typed Faker class is great to create complex builder scenarios, the power of defining the shape of the type in parts as necessary is one of the best things to solve problems like test scenarios.
Something like this:
Things get a bit complicated when the Type has a complex constructor or a mix with required properties:
In this case, we kinda lost the power of member composability. If it was a custom builder I would have to create fields to hold values for the
CustomInstantiator
etc.So would be nice to have a way to get the shape of the data without constructing the actual Type
A generic way to solve this could be to have a new method like
GetJsonObject
, which for the case above could be something like:So it could be used on the complex constructor, preserving the member composability:
Another solution would be use Castle
DynamicProxy
to maintain the type contract butmocking
the property values:Is the feature something that currently cannot be done?
I don't think so
What alternatives have you considered?
Customize the Faker inheriting from it, or just create/initialize all the properties manually
Is this feature request any issues or current problems? No
Has the feature been requested in the past? No
If the feature request is approved, would you be willing to submit a PR?
Yes
The text was updated successfully, but these errors were encountered: