vendredi 1 novembre 2019

Unit-testing of custom NLog filter class

While implementing custom NLog filter https://github.com/NLog/NLog/wiki/Filtering-log-messages#fully-dynamic-filtering I tried to rely on WhenMethodFilter, which accepts lambda callback ShouldLogEvent. However to unit test that callback lambda I need to make it public in the class that generates the config, which is not idea - I hate making methods public just for the sake of testing.

    private void ReconfigureNlog(object state)
    {
        var nlogConfig = ConstructNlogConfiguration();
        foreach (var rule in nlogConfig.LoggingRules)
        {
            rule.Filters.Add(new WhenMethodFilter(ShouldLogEvent));
        }

        // TODO: maybe no need to reconfigure every time but just modify filter reference?
        NLog.Web.NLogBuilder.ConfigureNLog(nlogConfig);
    }

Another approach would be to inherit Filter base class and try to cover that class with unit tests. But the issue that it does not have a public interface:

internal FilterResult GetFilterResult(LogEventInfo logEvent)
protected abstract FilterResult Check(LogEventInfo logEvent);

which makes this class also not testable unless I make my own methods public. As much as this seems to be a small issue, I'm curious if there is a better way. From my perspective making GetFilterResult internal is absolutely unnecessary, although it kinda following best design practices. Thoughts?

Aucun commentaire:

Enregistrer un commentaire