mardi 30 juin 2015

Dependancy Injection in a static class

I am writing Unit Tests for an existing code base, and am still getting up to speed.

There is a static Logger class which is written like this (simplified):

public static class Logger
{
    public static void Log(string message)
    {
        var myService = new Service();
        myService.Save(message);
    }
}

I would like to be able to Mock (using Moq) the Service class so I can verify the result. But there is obviously no way to do that with the class as it exists. But I don't see how the Service dependency can be injected into the static class so that it can be mocked. Is there a way to do that? Or should it not have been written as a static class?

Aucun commentaire:

Enregistrer un commentaire