Skip to content

Latest commit

 

History

History
79 lines (57 loc) · 3.24 KB

File metadata and controls

79 lines (57 loc) · 3.24 KB

Bogus

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:

  1. Creating test data: Bogus uses the Faker<T> class, where T is the type of object you want to generate. You can define rules for generating data using the RuleFor method.
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());
  1. Generating instances: Once you've defined the rules for generating data, you can use the Generate method to create an instance of the object with the specified rules.
var customer = customerFaker.Generate();
  1. Generating collections: Bogus can generate collections of objects with the Generate method, which generates a specified number of objects with the rules you've defined.
var customers = customerFaker.Generate(5);
  1. 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());
  1. 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 Faker instance passed to the RuleFor method.
.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.