Dependency Injection in Static Class

I am writing Unit Tests for an existing codebase and am still getting speed.

There is a static class Logger, which is written this way (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 that I can check the result. But there is obviously no way to do this with a class as it exists. But I don't see how a service dependency can be injected into a static class so that it can be mocked. Is there a way to do this? Or was it not written as a static class?

This is different from this question because the service is not a static class.

+3
c # unit-testing moq


source to share


No one has answered this question yet

See similar questions:

21
Dependency Inclusion with Static Registrar, Static Helper Class

or similar:

2560
How can I test a private function or class with private methods, fields or inner classes?
1197
Static readonly vs const
633
What's the difference between faking, bullying and biting?
140
How do I mock a service that returns a promise in Angularjs Jasmine unit test?
77
Mocking HttpClient in unit tests
18
Mocking Inner Classes with Moq for Unit Testing
8
C # Static Readonly log4net logger, any way to change the logger in Unit Test?
4
Mock Nested Dependencies with MOQ
2
Unit test to check if base class method is called
1
Using MEF with Dependency Injection



All Articles
Loading...
X
Show
Funny
Dev
Pics