11
4
|
I am adding new functionality into a legacy application. For my new classes I have introduced Unit Testing. I still need to work with the existing code.
One of the problems I face is that a lot of static methods have been used.
Below is an example of a class I need to use:
My fix for making this testable:
A class that used to use the static method, but by introducing a property (initialized by the constructor), that now uses the testable class. I can inject a mock using this property. Note that I could have used a factory for injecting the mock, however I wanted to keep it simple for the example.
My advantages:
My disadvantages:
I welcome any feedback!
| |||
add a comment
|
11
|
I happen to like your fix for making it testable, but would reverse the implementation order between the
static method and the explicit interface method. This will allow you to slowly change existing code first to use the Singleton instead of the static and then finally use an injectable pattern:
| ||||||||
|
5
|
What about creating new classes that use the existing static methods rather than extending existing code. That way you will not need to worry about breaking existing code or adding duplicate method calls onto existing?
One thing I would say for certain is to stay away from a property name being the same as the class name. Name it something different at least. Even just Helper is better than HelperClass (although probably not much).
Something alot along the lines you mentioned but very slight differences:
And it is used like:
| ||||||||
|
1
|
Personally I've found explicitly implementing interfaces to be a world of hurt with difficult to track down bugs and odd debug views.
The Simplest solution (and the best one IMO when the method doesn't access or modify state):
then in your AssemblyInfo.cs
and just stub it out in your test however you want:
If you ARE reading/writing state then a singleton would probably serve best
The only option that will allow you not to change ANY of the existing code at all is to use a super-powered mocking framework like Typemock Isolator (or Moles) which can mock out just about anything, including statics.
| ||||
|
from : https://codereview.stackexchange.com/questions/10359/unit-testing-legacy-code-with-static-classes