https://github.com/bchavez/Bogus
Bogus is a powerful and flexible library for .NET that helps you generate realistic test data for your unit tests. It uses a fluent API to create data based on user-defined rules, allowing you to generate data that closely resembles real-world scenarios. Bogus supports a variety of data types, including names, addresses, dates, numbers, and more, and it can also be localized for different regions and languages.
Here are some key concepts and features of Bogus:
- Creating test data: Bogus uses the
Faker<T>class, whereTis the type of object you want to generate. You can define rules for generating data using theRuleFormethod.
var customerFaker = new Faker<Customer>()
.RuleFor(c => c.Id, f => f.Random.Number(1, 1000))
.RuleFor(c => c.FirstName, f => f.Name.FirstName())
.RuleFor(c => c.LastName, f => f.Name.LastName());- Generating instances: Once you've defined the rules for generating data, you can use the
Generatemethod to create an instance of the object with the specified rules.
var customer = customerFaker.Generate();- Generating collections: Bogus can generate collections of objects with the
Generatemethod, which generates a specified number of objects with the rules you've defined.
var customers = customerFaker.Generate(5);- Localization: Bogus supports generating data for different regions and languages using the
Faker<T>constructor. Pass in the desired locale code to generate data for that region.
var customerFaker = new Faker<Customer>("es")
.RuleFor(c => c.FirstName, f => f.Name.FirstName())
.RuleFor(c => c.LastName, f => f.Name.LastName());- Built-in data types: Bogus provides built-in generators for a wide variety of data types, including names, addresses, dates, numbers, and more. You can access these generators through the
Fakerinstance passed to theRuleFormethod.
.RuleFor(c => c.Email, f => f.Internet.Email())
.RuleFor(c => c.BirthDate, f => f.Date.Past(30, DateTime.Now.AddYears(-18)))
.RuleFor(c => c.Phone, f => f.Phone.PhoneNumber());Here's an example of using Bogus to test the CustomerService class:
using Bogus;
using Xunit;
public class CustomerServiceTests
{
[Fact]
public void GetCustomerFullName_CustomerExists_ShouldReturnFullName()
{
// Arrange
var customerFaker = new Faker<Customer>()
.RuleFor(c => c.Id, f => f.Random.Number(1, 1000))
.RuleFor(c => c.FirstName, f => f.Name.FirstName())
.RuleFor(c => c.LastName, f => f.Name.LastName());
var customer = customerFaker.Generate();
var repository = new MockCustomerRepository();
repository.AddCustomer(customer);
var customerService = new CustomerService(repository);
// Act
string fullName = customerService.GetCustomerFullName(customer.Id);
// Assert
Assert.Equal($"{customer.FirstName} {customer.LastName}", fullName);
}
}In this example, we use Bogus to generate a Customer instance with realistic data. The CustomerService class is then tested using the generated data. This demonstrates how Bogus can help you create more realistic and diverse test scenarios.