question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't have a Bogus FinishWith configured

See original GitHub issue

Since Bogus allows only a single FinishWith method, the way AutoBogus works its magic makes it impossible for someone to configure a FinishWith for a Type like this:

public class PersonBuilder : AutoFaker<Person>
{
//    ... props
}

// ...
var personBuilder = new PersonBuilder().FinishWith((f, p) => 
{ 
    // something 
})
.Generate();

This is common in a scenario where a property doesn’t have a setter and has to be configured through methods. So something like this:

public class PersonBuilder : AutoFaker<Person>
{
    public PersonBuilder WithAddresses(int count = 1)
    {
        // store how many addresses must be generated
    }

    public PersonBuilder WithEmails(int count = 1)
    {
        // store how many emails must be generated
    }

    // other methods for Collections without setter

   public override Generate(string ruleSets = null)
   {
        FinishWith((f, p) =>
        {
            // Calls methods that populate Addresses, Emails, etc.
        });
   }
}

From what I looked at the code, the culprit is this line.

I see a few ways this could be fixed: Bogus could allow multiple finalizers (I don’t see why this isn’t allowed anyway), or AutoBogus could execute that code in a different way.

What do you think? Is this something that should be improved?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
nickdodd79commented, Jan 29, 2020

Hey @metareven

Thanks for reminding me of this one. I had made the changes a while ago, just hadn’t release it.

This capability has now been included in v2.8.

Nick.

2reactions
bchavezcommented, Sep 27, 2019

Hey @nickdodd79 @akamud,

I really apologize for the delay. I’ve been busy preparing for job interviews at a few places. Also been thinking a lot about moving Bogus to the .NET Foundation as part of their new Project Maturity Model announced early this week at .NET Conf; mostly as a move to gain more users and as a way of establishing confidence for bigger businesses. @nickdodd79 , if you’d like to be a part of it and you think it’s right for you, please send me an email and we can talk more about it. Essentially, PML Level 3 and 4 require more than 1 maintainer on the project.

As for Bogus having multiple .FinishWith methods, I’m not sure. The conceptional API usage for Faker<T> is that the last-call wins. IE:

  • Faker<T>.RuleFor(x.Prop, f => 1) and then if the same rule for the member is specified again,
  • Faker<T>.RuleFor(x.Prop, f => 2) that f => 2 wins and f =>1 rule is over-written.

The same conceptual API consistency applies to .FinishWith( f, t =>); last-call wins.

At least from Bogus’ point of view; I’m not sure how helpful keeping track of past rules is important to Faker<T> other than in this specific case here.

  • Do we modify Faker<T> to allow this implementation detail scenario? I’m not sure yet but inclined to think maybe not, but I’m open to discussing more. My mind can always be changed tho.

I like method 3 that you mentioned as a possible workaround for this issue is for AutoBogus to store a reference to any finalizers being kept track in Faker<T> and call them after AutoBogus is done hooking FinishWith. For sure, I can help with this by opening up some kind of accessor to a member if anything is private. But it seems like we already have that protected accessor set: https://github.com/bchavez/Bogus/blob/master/Source/Bogus/Faker[T].cs#L54

PR if you need anything more opened if it could help your implementation.

Lastly, another possibility if we want to get crazy hard-core about it; any Action is really a delegate under the hood; specifically, a multi-cast delegate? So, as a crazy C# hack, you technically can combine two Action methods in C#; like this check it out:

void Main()
{
   Action a = () => "Hello A".Dump();
   Action b = () => "Hello B".Dump();
   Action c = a + b;
   c();
}
Hello A
Hello B

IIRC, under the hood, the C# compiler will use Delegate.Combine(a,b) to produce c. Thereby hacking your way around this neat little problem of needing to keep track of a ‘list’ of invocations. 😈 Specifcally, get the FinalizeAction.Action and attach your custom hook … might work?

Let me know your thoughts.

Thanks, Brian

🗻 🤽‍♂️ Creedence Clearwater Revival - Green River

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bogus
Bogus is a simple fake data generator for .NET languages like C#, F# and VB.NET. Bogus is fundamentally a C# port of faker.js...
Read more >
Bogus : Simple fake data tool | Sacha's Blog - WordPress.com
Bogus : Simple fake data tool ... //Set the randomzier seed if you wish to generate repeatable data sets. ... //Use an enum...
Read more >
Difficulty using usethis::use_github() - tidyverse
Following the documentation for the function, I first setup git using usethis::use_git(). ... repo_name),; paste0("Description: ", repo_desc),; copy = FALSE ...
Read more >
URLSession background fails with t…
sessionSendsLaunchEvents = false configuration. ... but it only for testing, I can't really detect when the app has been quit from the app...
Read more >
Pixel setup in progress. Tap to finish. Showing for a few days
Tap to finish. ... For the past few days my phone has been displaying "Pixel setup in progress. ... false. Enable Dark Mode....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found