Skip to content

ServiceCollector.Fake.Configuration

Ershad Raoufi- ارشاد رئوفی edited this page Jun 8, 2024 · 7 revisions

BaseGenerator Class:

The BaseGenerator class provides a mechanism to create instances of objects with fake data using the AutoFixture library customized with AutoNSubstitute.

FakeConfiguration Class:

The FakeConfiguration class is used to configure a fake service instance. This class is designed to help set up and customize the behavior of a fake service for testing and development purposes.

namespace ServiceCollector.Fake.Configuration
{
    public class FakeConfiguration where TService : class
    {
        public TService Service { get; }

        public FakeConfiguration(TService service)
        {
            Service = service;
        }
    }
}

Description:

The Service property holds the instance of the fake service being configured. This property allows access to the fake service so that its behavior can be customized as needed.

Type TService: The type of the service being faked.

FakeConfigurationWithMultiEnvironment Class :

The FakeConfigurationWithMultiEnvironment class allows the configuration of fake services for multiple environments.

This class provides a mechanism to set up different fake implementations of a service based on the target environment, facilitating testing and development across various stages (e.g., Development, Staging, Production).

namespace ServiceCollector.Fake.Configuration
{
    public class FakeConfigurationWithMultiEnvironment
        where TService : class
    {
        private readonly string _currentEnv;
        public IDictionary Services { get; private set; }

        public FakeConfigurationWithMultiEnvironment(string currentEnv)
        {
            _currentEnv = currentEnv;
            Services = new Dictionary();
        }

        public void Add(string targetEnvironment, Action service)
        {
            var serviceObject = BaseGenerator.Create();
            service(serviceObject);
            Services.Add(targetEnvironment, serviceObject);
        }

        public void Add(Action service)
        {
            var serviceObject = BaseGenerator.Create();
            service(serviceObject);
            Services.Add(_currentEnv, serviceObject);
        }
    }
}

ServiceConfigExtension

The ServiceConfigExtension class provides extension methods for configuring fake services in your application using the IServiceConfig interface.

These methods facilitate testing and development by allowing you to replace real service implementations with fake ones under specified environments.

FakeInMultiEnvironments method :

public static IServiceConfig FakeInMultiEnvironments(
    this IServiceConfig serviceConfig,
    Action> action,
    string currentEnvironment = "Development")
    where TService : class

Parameters:

serviceConfig: The IServiceConfig instance to which the fake service will be added.

action: An action to configure the fake service for multiple environments.

currentEnvironment: The environment in which the application is currently running (default is "Development").

Returns:

IServiceConfig: The same IServiceConfig instance with the fake service configured for the specified environment.

serviceConfig.FakeInMultiEnvironments(config =>
{
    config.Add("Development", service =>
    {
        service.DoSomething().Returns("Development Fake Result");
    });

    config.Add("Staging", service =>
    {
        service.DoSomething().Returns("Staging Fake Result");
    });

    config.Add("Production", service =>
    {
        service.DoSomething().Returns("Production Fake Result");
    });
});