Dependency breaking techniques from Michael Feathers book
This has nothing to do with Ruby as such. But I needed a place to keep this checklist of Working Effectively with Legacy Code interesting Dependency breaking techniques.
Adapt parameter - Shim Wrapper class around a awkward collobarator. Think HttpServletRequest...
Break out method object - Move large method to a class of its own with the local variables as the instance variables of the class and the method's argument initially is the host class. What is the point? It now helps us re-factor that class to expose its innards for extracting interfaces..
Expose static method - If the code under test does not need instance variables or methods to execute, expose that code as static and now you can test it lot more easily.
Extract and override call, factory method, getter - All about moving the awkword collobarator code into a method of its own and overriding it using subclass in test.
Extract implementer - Primarily for creation of clear names, push code from perfectly named class down one level and make the current class an interface.
Extract interface - Use tool support if available, safest way to break dependency. We saw problems where there are hierachies of objects and abstract classes in between current class and Object. Probably, Extract Implementer.
Introduce instance delegator - Basically useful for breaking dependency on a awkword static method. Create a instance method with same signature as the static and delegate. Replace the calls to static method with the instance delegator.
Introduce static setter - Seems like a way to get around static methods to work with singletons (such in Service classes). Basically, you need a static setter and make the c'tor for the object protected. Then we can construct the object by deriving off of the main object and setting that as the value of the singleton under test.
Parameterize constructor - Allow passing in objects that are normally created within the object. AKA Dependency injection.
Parameterize methods - pass in as arguments, objects that are used within a method.
Primitivize parameter - Basically move the code around so that the actual logic getting executed is using primitive types (ints, longs or in Java some of the primitive objects). Dangerous since eventhough it gets code to test, exposes internal details, duplicates data and also prolongs the problem.
Pull up feature: Useful if you have class that has dependencies that causes problem when instantiated because of dependencies. Pull up the class of methods to be tested to a superclass and derive the old class from this new super class. Test class also derives from the new super class and now you have access to those methods and can instantiated cleanly.
Push down dependency: This is similar to extract interface or subclass and override. Here we push down the problematic dependencies down to a subclass. This is opposite to pull up in that in pull up we are trying to get the methods out of the current class because there are many other methods that cant be tested because of dependencies and in push down, there are only few dependencies that are specific to some collobrarator that can be pushed down. This might need the callers to be modified.
Subclass and override: extention of extract and override.


0 Comments:
Post a Comment
<< Home