nickdodd79 / AutoBogus

A C# library complementing the Bogus generator by adding auto creation and population capabilities.
MIT License
431 stars 50 forks source link

WithOverride does not seem to work #33

Closed metareven closed 4 years ago

metareven commented 4 years ago

Hi!

I am trying to use overrides to generate data, but when I log the contents of context.Instance inside my Generate function, and what is actually returned from faker.generate() it is 2 completely different things.

I have a bunch of classes who inherit from a metadata class and I wanted to use overrides to automatically set the fields contained in the metadata class whenever one of its subclasses generated.

This is the code from the function that calls the Generate function:

var faker = new AutoFaker<Folkeregisteridentifikator>()
    .RuleFor(fake => fake.FoedselsEllerDNummer, fake =>
        fnr ?? new Bogus.Person().Fødselsnummer());

var fake = faker.Generate();

And this is the code in the override function:

var data = context.Instance as dynamic;

data.Gyldighetstidspunkt = new Faker().Date.Recent(0);
data.Opphoerstidspunkt = new Faker().Date.Future();
data.Ajourholdstidspunkt = data.Gyldighetstidspunkt;

When printing out the objects in the different functions I get this output:

Generated from override
{"Status":1,"FoedselsEllerDNummer":"salmon","Identifikatortype":1,"AjourholdstidspunktValue":"2019-08-08T00:55:25.459591","AjourholdstidspunktValueSpecified":true,"Ajourholdstidspunkt":"2019-08-08T00:55:25.459591","ErGjeldendeValue":false,"ErGjeldendeValueSpecified":true,"ErGjeldende":false,"Kilde":"Cambridgeshire","Aarsak":"Industrial, Baby & Home","GyldighetstidspunktValue":"2019-08-08T00:55:25.459591","GyldighetstidspunktValueSpecified":true,"Gyldighetstidspunkt":"2019-08-08T00:55:25.459591","OpphoerstidspunktValue":"2020-04-16T06:03:23.6211179","OpphoerstidspunktValueSpecified":true,"Opphoerstidspunkt":"2020-04-16T06:03:23.6211179"}

Returned from faker.generate
{"Status":1,"FoedselsEllerDNummer":"24095138264","Identifikatortype":0,"AjourholdstidspunktValue":"2019-08-08T06:01:04.3078119","AjourholdstidspunktValueSpecified":true,"Ajourholdstidspunkt":"2019-08-08T06:01:04.3078119","ErGjeldendeValue":true,"ErGjeldendeValueSpecified":true,"ErGjeldende":true,"Kilde":"blue","Aarsak":"Credit Card Account","GyldighetstidspunktValue":"2019-08-08T08:41:16.0475949","GyldighetstidspunktValueSpecified":true,"Gyldighetstidspunkt":"2019-08-08T08:41:16.0475949","OpphoerstidspunktValue":"2019-08-07T16:23:50.9884737","OpphoerstidspunktValueSpecified":true,"Opphoerstidspunkt":"2019-08-07T16:23:50.9884737"}

As you can see, the values are completely different, and it also seems like the object inside the generator function does not follow the rules I set when making the faker

nickdodd79 commented 4 years ago

Hey @metareven

I will need to investigate why you are see what you describe. Watch this space and I will let you know what I find and when a fix is in place.

Nick.

nickdodd79 commented 4 years ago

Hey @metareven

I have just put a fix in for another issue and think it may have indirectly fixed what you detail above. Can you try v2.8.2 and verify it works for you. If not, I will continue investigating.

Thanks, Nick.