Forwarding and Dependency Injection
This morning we had a workshop on Forwarding, which it turns out is the same as Delegation which I covered yesterday. The task we were given was a little tougher though so I still tried to complete it. At first I got myself all confused, and nothing was working. I think it was because we were given some code to start with, some tests we couldn’t change, and some old non cohesive code to base it on. I wasn’t sure how much to change and how much to leave as it was. I decided I was in too much of a mess, so started again. It didn’t take long after that to get it working as intended! Looking at the solution file, mine was fairly close, there was some more forwarding and refactoring I could have done, but it was mostly the same.
I am happy with the idea of Forwarding, and I am fairly confident in the implementation of it (mostly), but I would like to get more of a handle on how to test it properly, which is the topic of tomorrows workshop. I know you need to use doubles, and stub methods, but I am not sure of the best way to do it.
I went to start on Oyster Cards again, however the challenge I am on relied heavily on dependency injection, which we have not covered yet. So instead I started a practical focussed on that topic!
Here is my finished repo for the practical. To be honest I found it quite difficult, and I wouldn’t say I am overly confident on it now, but I did manage to complete it on my own with no help from coaches or google, which I am happy about. Basically it was about writing code that depends on variables or methods injected from other classes, and then writing tests for that code, where it is unaffected by changes to the classes. So you have to use Doubles to set variables to ensure that only the class in question is really being tested. The idea is that if a class breaks, it shouldn’t break all the tests for other classes that rely on it. Those classes have their own tests to make sure they work, they should be able to assume the class they rely on will work every time. Adding doubles into tests means that even if you were to delete everything in that class, your tests would still work! See below an example:

My song of the day is an old favourite of mine.

