'Is it possible to gain code coverage for constructors with unit tests?

Say I have these private fields and this constructor...

    private readonly ILogger<KafkaFactory<T>> _logger;
    private readonly IKafkaConfig _kafkaConfig;
    private readonly IOptionsMonitor<IKafkaConfig>? _kafkaConfigMonitor = null;
    private readonly string? _configName = null;
    private IKafkaConfig Options => _configName == null ? _kafkaConfigMonitor?.CurrentValue ?? _kafkaConfig : _kafkaConfigMonitor!.Get(_configName);

    public KafkaFactory(ILogger<KafkaFactory<T>> logger, IOptionsMonitor<IKafkaConfig> kafkaConfig, string name)
    {
        _logger = logger;
        _kafkaConfigMonitor = kafkaConfig;
        _configName = name;
        _kafkaConfig = kafkaConfig.Get(name);
        _logger.LogInformation("{class} using named options {options}", nameof(KafkaFactory<T>), Options);
    }

How do I cover this code with unit tests? Is this even possible?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source