In today’s newsletter we are going to discuss
Why we need to generate fake data
What is the Bogus and benefits of using it
How to use it in .NET Core
Why we need to generate fake data
While working with our applications we need to generate fake data for different purposes.
For testing purpose (especially unit testing)
For demonstration of data maybe for charts/graphs
Dumping the data in databases
What is the Bogus and benefits of using it
If you come from JS world then you would be aware about faker.js
Bogus is C# version of that library.
Bogus is a Nuget package available on Nuget Package Manager with approximately 46M downloads.
These are the benefits of Bogus
Compatible with C# , F# and VB.NET
Supported in 50 international languages
It comes with a variety of data e.g. addresses,commerec,companies,database,finance,dates,images,vehicles and many more
It has some country based extensions as well for different countries (e.g. Brazil,Norway,Finland,Italy and few more)
It has premium packages as well for movies,tvs,literature,medical and diagnosis details
It gives us the facility to apply variety pf rules on data according our need
Its really simple to use it and you don’t need any lengthy process , just install package and use it
How to use it in .NET Core
Step 1 : Install Nuget Package with name Bogus (With 45.3 M Downloads)
Step 2 : Make rules for your fake model
Step 3 : Call Generate
method of library to generate data
Let’s see with code now for better understanding
Simple Demonstration of Bogus
In this example you can check all list of properties/methods available in library in second part of expression(RuleFor
)
How to Generate N’ Fake Entries
We can pass an integer in parameter to Generate
method and it will create that much entries.
How to Generate Same Data Every Time
Bogus gives us the facility to create same data every time by defining a random number of our choice like this .
How to Generate Data in Other Languages
By default data is generated in “en” but you can pass language code of your choice to generate data in your language . Check language list from Bogus GitHub Repo
This is a just basic overview of library , covering each and everything is not possible , I would recommend you to visit GitHub repository of the library and you can find the things which you need.
Whenever you’re ready , there are 4 ways I can help you
Promote yourself to 5400+ subscribers by sponsoring my Newsletter
Become a Patron and get access to 130+ .NET Questions and Answers
Get FREE eBook from Gumroad that contains 30 .NET Tips (2500+ downloads)
Book a one hour 1:1 Session and learn how I went from 0-29K on LinkedIn in 8 months , Book here
Special Offers 📢
Ultimate ASP.NET Core Web API Second Edition - Premium Package
10% off with discount code: 9s6nuez