It is very likely that sooner or later we will have to face code that has two characteristics:
- Untested code
- Highly coupled code
What should we do first: the test, or the refactoring to be able to test better? The egg or the hen? We already know why not to use static, but it may be that "our self from a year ago," or even that project we've joined, makes use of these types of practices. It's possible that even that project has a large base of untested code. But this shouldn't be a problem for us; we have watched the CodelyTV videos and we are trained for this and more :D In today's video, we will see a practical application of the concept of seam. This concept is related to a technique that can be useful when navigating through code that has the two aforementioned characteristics (coupled and untested). This technique will help us modify our code as little as possible in order to make it easier to test. The procedure will be as follows:
- Apply the technique we will see in the video (open a seam)
- Develop our test focused on the component we want to test while ignoring the collaborating classes it is coupled with
- Deeply refactor, now with the peace of mind of the test we have been able to implement
As I mention in the video, I discovered this technique at a Barcelona Software Craftsmanship meetup thanks to Manuel Rivero. If I’m not mistaken, it was cited from the book "Working Effectively with Legacy Code". I would also like to mention the original post on which this video is based: Testing legacy: Hard-wired dependencies (by Sandro Mancuso). Finally, remember that these types of couplings can be easily identified if we know how to listen to our tests, that mysterious ship we talked about some time ago :)