Tuesday, July 10, 2018

Creating Fake Data For Unit Tests With RimuTec.Faker

Setting up data for tests can be tedious. For example sometimes you may end up using first names like “First 1”, “First 2”, etc. Data like this may result in duplicates, so you then go to the next step using first names like “First “+ Guid.NewGuid() which may be a problem, too. Using such a name in a user interface may no longer fit the available space, where real-life names like “John” or “Anna” would fit easily.

Another aspect can be that you are required to have a most recent version of your system or product for demo purposes, let’s say a pre-sales situation or an internal product review. Trying having a conversation about a screen that says “First123456 Last645861”. Wouldn’t it be nice if it would display names such as “John Doe” or “Anna Karenina”? It most likely would also create a much better impression with prospects, customers or other stakeholders. Your software looks more “complete”.

Ruby developers have a gem named Faker which is available on github. Faker is based on a Perl library and has a large library of fake data generators.

There have been a few efforts to port the Ruby gem to .NET. Most of these have a limited set of generators and haven’t been maintained for years.

Therefore we decided to have another look. Where other projects use regular resource files, we decided to use the yaml files from Ruby’s Faker gem verbatim. By taking this approach we would automatically have some guardrails that would allow leveraging as many of the great ideas from the Faker gem as possible.

The result is RimuTec.Faker which offers the following classes of generators:
  • Address
  • Company
  • Educator
  • Finance
  • IDNumber
  • Internet
  • Job
  • Lorem
  • Name
  • PhoneNumber
  • RandomNumber
Obviously this is not the complete list yet. We release frequently and are happy to receive suggestions for priorities in terms of what should be added next. Just log an issue on github.

Using RimuTec.Faker the example from the beginning be written as follows:

You can get both, the source code as well as the NuGet package under the MIT license. This means you can use it for free and also use it in your own projects as you see fit, including commercial and closed-source projects.

Happy coding!