I work with many Proof Of Concepts (PoCs) these days and one of the things I find in need frequently is the ability to generate large collections of objects that may resemble what our customers expect. For obvious reasons, I can't grab a copy of a customer's database or access their Azure resources. So I need an efficient, reliable and fast way to generate fake but live-like data that I can use to test my code.
Over the years I've used many different tools to do this but lately, I've grown extremely fond of [Bogus](https://github.com/bchavez/Bogus" target="_blank) by Brian Chavez. I find this library extremely handy and well thought out and it surely deserves a shout out considering how frequently I use it. Brian is also an awesome maintainer. When I raised an issue asking for a UK SortCode (UK bank routing code) to be added to the
Finance API, he reached out with one of the most comprehensive guides on how to contribute! If not for anything else, he definitely deserves a huge congrats for this. Now, enough with the praises, back to our blog post!
Bogus has excellent support for both the full .NET and .NET Core frameworks and it's actively maintained so you can rest assured that it's up to date. It provides both a fluent and non-fluent API to appease all developer tastes and styles. For the purpose of this post, I'll use a .NET Core Console application.
Open the command line and create a new .NET Core project
mkdir bogusdemo && cd bogusdemo dotnet new console
dotnet to do its thing and the CLI to restore the packages. Remember that .NET Core 2.0 does this implicitly at
dotnet new since most people (including me) would totally forget to run the
dotnet restore command. Fun times!
To add NuGet packages from the CLI you need to issue the
dotnet add package <package name> --version <versionNumber>. If you need to find more information about how to use a specific
dotnet command, remember you can use help command. For example,
dotnet add --help will show the following:
Consequently, to add the Bogus NuGet package to our console application, we need to run the following command:
dotnet add package Bogus --version 22.0.2
With this out of the way, we can now create fakes as needed. In this instance I want to create a number of records that match the schema of the AdventureWorks.SalesLT.Customer database table. Therefore, my POCO class needed to look like this:
The plan is to use this class (later) to populate my [Azure Storage Table](https://azure.microsoft.com/en-us/services/storage/tables/" target="_blank). That's why the class inherits from
TableEntity and there's a Partition/Row key combo. But let's leave this aside for now. The next step is to create the fake/sample data. I wanted 1000 rows so this was a piece of cake with Bogus:
Let's break it down a bit here. The main object
Faker get's passed a strongly-typed object
TableUser and then we provide the rules for building our fakes. By default Bogus uses strict mode to enforce that all object parameters are assigned roles. You can overwrite this by setting StrictMode to false using the fluent API
You may also notice that Bogus allows us to instantiate objects with non-default constructors using the
CustomerInstantiator() method. I'm sure this can be extremely handy in many scenarios. Each property is then assigned a rule on how it should be populated by Bogus. Brian has done a great job providing APIs for various object fakes such as Person, Internet, Company, Finance and even Images! And in the case that the built-in APIs can't help you, you can be creative and use the
Replace function to generate random numbers, string or a mix of both. The
Replace() method replaces a formatted string with random numbers #, letters ?, or * random number or letter:
Var explicitNumbersOnly = new Bogus.Randomizer().Replace("###-##-####"); var explicitNumberAndLetterMix = new Randomizer().Replace("##? ??? ####"); Var implicitNumberAndLetterMix = new Randomizer().Replace("**-****");
To generate the desired number of fakes, you call the
In any case, the documentation is great and easy to follow and for the slightly unconventional cases, there are unit tests to help you navigate the tricky bits.
Hopefully you will find this library for generating fakes for your code as useful as I do. I find it invaluable and a tool that I turn to whenever I need to generate some data. The library works also with F# and VB.NET and there's even a [faker.js](https://github.com/marak/Faker.js/" target="_blank) for all of you working with front-end frameworks. Give Bogus a go and don't forget to ping Brian to let him know how you use this little yet extremely powerful library.
What interesting libraries do you use in your code?