I can't test this code because it's legacy. I need to update the code to make it testable. How can I manage that, without breaking existing functionality? I'll need to write some tests, but... argh!

Comments

Comments are closed.

So helpful and well explained. I really appreciated the insight into a topic that I am actively working on growing into.

John Congdon at 11:11 on 4 Nov 2022

Lots of great examples of tricky testing scenarios. I loved the way you handled the exit problem, as we have that issue in our current legacy application. We will probably end up using your idea from that.

While the bolognese example while being funny when talking about spaghetti code, it was a little convoluted and hard to follow.

Also wanted to share there's another way to handle static calls as well with Dependency Injection which is kind of your theme in the presentation.

We call them wrappers. I think I have your example right from me recollection. Example:


/**
* @method getPowerLevel(): int
*/
class GokuWrapper
{
public function __call($name, $arguments)
{
return Goku::$name(...$arguments);
}
}

GokuWrapper can now be injected and mocked.

Good code examples that illustrated the problems, and good to see how to refactor code to make it testable. Loved the humor and movie references, good flow, easy to follow along with, and overall a great talk!

Joe Theuerkauf at 09:48 on 5 Nov 2022

Really good examples and grail increase in difficulty level. i'm not sure i agree with adding function parameters specifically used by tests, but i can definitely understand that some legacy situations will demand it, and it DOES introduce a path to refactoring into testable code.

Joe Theuerkauf at 09:50 on 5 Nov 2022

Really good examples and gradual increase in difficulty level. i'm not sure i agree with adding function parameters specifically used by tests, but i can definitely understand that some legacy situations will demand it, and it DOES introduce a path to refactoring into testable code.