How To Generate Fake Data With Bogus in .NET 6
Tags: csharp, dotnet, dotnetcore, tutorial
Published: 2023-06-21 16:04:32

Bogus is a popular open-source library for generating fake data in .NET applications. It allows us to create realistic-looking, randomized test data for various purposes, such as unit testing, database seeding, or generating sample data for demonstrations.

In .NET 6, we can easily incorporate Bogus library into our projects by adding it as a NuGet package. Once installed, we can leverage its extensive set of data generation capabilities to create realistic and diverse fake data.

---

## Key features and benefits

- **Data Population:** Bogus provides methods for easily populating objects and collections with fake data. This is particularly useful for database seeding or creating large datasets for performance testing.

- **Realistic Data:** The generated data from Bogus is designed to resemble real-world data. It follows patterns, distributions, and variations typically found in actual data, helping us create more accurate test scenarios and sample datasets.

- **Rich Data Generation:** It provides a wide range of data generators that can create fake data for common types like names, addresses, phone numbers, email addresses, dates, numbers, and more. It also supports generating custom data types using fluent API extensions.

- **Localization Support:** It supports generating data in multiple languages, making it suitable for internationalization testing or multi-lingual applications. It includes built-in localization for various countries and allows us to define our custom locales.

- **Customization Options:** We can customize the generated data to match specific requirements. Bogus allows us to control the format, constraints, and patterns of the generated data, ensuring it aligns with our application’s needs.

- **Integration with Entity Framework Core:** If you’re using Entity Framework Core in your .NET 6 project, Bogus includes built-in integration to help you generate fake data for your database entities and seed your database with realistic test data.

---

## How to use

Install Nuget Package [Bogus](https://www.nuget.org/packages/Bogus/)

```
Install-Package Bogus

```
Let’s use a **User** as an example

```
public class User
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public string? LastName { get; set; }
    public string? Country { get; set; }
    public string? Phone { get; set; }
}
```
Next step we just need to configure how the data will be generated.

The first parameter of the **RuleFor** method is a lambda to pick the property on **User** that we want to fake. The second parameter is another lambda to pass in how to generate the property.

```
public class UserRepository
{
    public List<User> GetSampleData()
    {
        var userId = 1;

        var userFaker = new Faker<User>()
            .RuleFor(u => u.Id, _ => userId++)
            .RuleFor(u => u.Name, f => f.Name.FirstName())
            .RuleFor(u => u.LastName, f => f.Name.LastName())
            .RuleFor(u => u.Country, f => f.Address.Country())
            .RuleFor(u => u.Phone, f => f.Phone.PhoneNumberFormat());

        var users = userFaker.Generate(1000);

        return users;
    }
}
```
To use the **Faker**, we just need to call **Generate** method with how many users we want.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/958iswz3dbsy8aij2yzy.png)

As we can see 1000 users were generated using realistic names. This is because Bogus contains a set of generalized rules for common data categories (i.e. Names, Addresses, Companies, People, Phone Numbers, etc.)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwqbhjbitu5xeufdx5i9.png)


With Bogus, we can generate data in more than 45 languages.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpkss9f85rejqbi9thp7.png)


We just need to pass the locale code inside the constructor.

```
var userFaker = new Faker<User>("es")

```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ujixjidcofbg3eif7j4f.png)

_For more info https://github.com/bchavez/Bogus_

Happy Coding :)

---

## Final Thoughts
Overall, Bogus is a powerful tool that simplifies the process of generating fake data in .NET 6 applications. It enables us to create diverse, realistic, and customizable test data, saving us time and effort in the development and testing process.

---

## Thanks for reading!
Through my articles, I share Tips & Experiences on web development, career, and the latest tech trends. Join me as we explore these exciting topics together. Let’s learn, grow, and create together!

➕More article about [Programming, Careers, and Tech Trends.](https://medium.com/@andytechdev)

🔔 Follow me on [Medium](https://medium.com/@andytechdev) | [Twitter ](https://twitter.com/andytechdev)