I'm trying to generate a production-quality and -quantity size test data set with Bogus, and this library works extremely well with basic data - simple datatypes like int
or string
, things like first and last name etc.
I'm currently not seeing how I can handle two scenarios in my test data setup:
for certain attributes of an object, I'd like to be able to define something like "in 20-30% of the cases, use a NULL
instead of generating a value" - is this possible somehow?
in other cases, I need to randomly pick an object from a list of available objects - but I need to use that one, picked object to set more than one attribute on my object being generated. E.g. for an "order", I might want to pick a "City" from a given list of possible cities - and once I have a city, I want to set the CityName
, State
and ZipCode
of my "order" object from that one, selected city. I haven't found a way to do this (yet) - any takers?
To provide null sometimes you can just pick random number in 0-100 range and generate value only if it's above threshold:
// generate null in 30% of cases
RuleFor(o => o.Item, f => f.Random.Number(0, 100) >= 30 ? f.Name.FullName() : null)
To use picked object - just use this property in followup rule:
// u in lambda represents whole object
RuleFor(o => o.City, f => f.PickRandom(cities)).
RuleFor(o => o.CityName, (f, u) => u.City.Name)