---
title: "Instance Delegator Focus"
url: "https://courses.osherove.com/courses/legacy-code-hero/instance-delegator-focus.md"
canonical: "https://courses.osherove.com/courses/legacy-code-hero/instance-delegator-focus/"
tags: []
author: "Roy Osherove"
description: "Lesson 3 of 5 in Legacy Code Hero (Refactoring), 13:44. Free video by Roy Osherove."
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Instance Delegator Focus

1. [Courses](https://courses.osherove.com/#courses)
2. [Legacy Code Hero](https://courses.osherove.com/courses/legacy-code-hero/)
3. Refactoring

Lesson 3 of 5

Duration: 13:44 [Watch on YouTube](https://www.youtube.com/watch?v=Ae2OzBGvl7I)

In this video Roy Shows specifically, and a bit more slowly, how he uses Instance Delegator to refactor the Manager class in BlogEngine.NET

## In this lesson

In BlogEngine.NET, Roy Osherove looks at the static Send method of a manager class, which scans a new post for links and sends trackbacks or pings, and explains why the static class and the blog settings singleton block [extract and override](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-3#ch03lev1sec5 "Variations on refactoring techniques (The Art of Unit Testing, 2nd ed., ch. 3)"). He copies the method into a non-static version, makes the static method forward to a new instance, and extracts virtual methods such as the enable trackback send check so a [testable subclass](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-4#sigil_toc_id_95 "Partial mocks (The Art of Unit Testing, 3rd ed., ch. 4)") can override them. He ends with an exercise: write [unit tests](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-1#sigil_toc_id_17 "Defining unit testing, step by step (The Art of Unit Testing, 3rd ed., ch. 1)") for when trackbacks and ping backs get sent.

## Transcript

1,834 words

[\[00:25\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=25s) So what we have here is the blog engine which we just downloaded from CodePlex. Our exercise for the for the rest of the day will be to write some tests for it. But first I'm going to show some another technique for working with [legacy code](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-12 "Working with legacy code (The Art of Unit Testing, 3rd ed., ch. 12)") that will help us get some this this thing under test. So because our first exercise will be about writing tests for this without anything like [Typemock](https://osherove.com/blog/2009/7/9/minimizing-unit-test-fragility-8-features-in-typemock-isolat.html "Minimizing unit Test Fragility – 8 features in Typemock Isolator to help (osherove.com)") isolator etc. then we're going to have to learn how to do some extra refactoring stuff.

[\[00:59\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=59s) Now, this project surprisingly actually does have some tests, but if we look at them, the tests are unusually simple or even simplistic. A lot of them are [integration tests](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-1#sigil_toc_id_25 "Integration tests (The Art of Unit Testing, 3rd ed., ch. 1)") actually. There's some tests for loading files and making directories.

[\[01:31\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=91s) There's some tests for creating posts and some tests actually use IE which means that these tests are integration tests that are using selenium if I'm not mistaken to run an instance of the browser and actually click buttons and links and then check various HTML on the page. So these are not [unit tests](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-1#ch01lev1sec1 "Defining unit testing, step by step (The Art of Unit Testing, 2nd ed., ch. 1)"), but they are very helpful when you're working with legacy code.

[\[02:03\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=123s) For now, we're not going to try to make these tests fast because we're missing all the libraries for running these integration tests. But you can take it as an exercise to try to get these tests to run. But later, not today. What we're going to do is we're going to write some tests for a blog engine manager. Nothing like a core manager class.

[\[02:34\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=154s) So this is actually a ping manager class our main interest in this class is the send method. So manager send is going to be our main interest here. So first of all let's talk about what the purpose of this is. Blog engine is a blog engine. You can install it and you can create your own blog. Blogs at the at the bottom of each post of a blog there is a thing there are a couple of sometimes we get automatic links that get posted into there.

[\[03:13\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=193s) These are called pings or trackbacks. Pings and trackbacks allow us to create instant conversations when someone else talks about us. And we can create instant conversation in someone else's blog by having a link to their blog in our blog. The the purpose of the send method here is to basically go through the HTML of a new post we just created and search for links to external blogs. And then for each one of them to try to send a ping to a special server out there which collects this information

[\[03:51\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=231s) And then that server saves the information and and when that when other blogs ask the server did someone talk about me it will say yes here's the link to whoever talked about you. So it's a way to connect blogs without talking to each other directly. pings and trackbacks only differ by the amount of information they collect if I if I'm not mistaken. But the basic idea is that we have blog settings whether trackback is enabled. We try to send a track back and if we can't send the track back or there's there was a problem where it's not enabled, we send the ping back.

[\[04:25\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=265s) So there are two possible [end results](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-1#sigil_toc_id_19 "Exit point types (The Art of Unit Testing, 3rd ed., ch. 1)") from this [unit of work](https://osherove.com/blog/2020/3/12/unit-testing-entry-points-and-exit-points "Unit Testing Entry and Exit Points (osherove.com)"). Okay, one end result is this. Another is this. So our main our main problem is going to be how to test that this was called when it was supposed to with the correct message or that this was called when it was supposed to with the correct message. And there are several things that are going to get in our way when we go through this

[\[04:59\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=299s) Because these methods are actually static. We cannot replace the instance of trackback to make sure it was called because send is a static method. Luckily, we can maybe use [extract and override](https://osherove.com/blog/2005/6/16/mock-objects-vs-quotinherit-amp-overridequot.html "Mock Objects vs. \"Inherit & Override\" (osherove.com)") to check that something got called. The same here. We also have track back sorry blog settings instance which is a singleton which we will probably need to break as a dependency as well because we don't want to use the real file system.

[\[05:30\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=330s) So, we could use extract and override here as well or create an interface with eye settings. But there's another problem. We can't extract and override these things because the class that we're in is a static class. It cannot have non-static methods. So, send is a static method. All the methods here are actually static methods like get track URL from page, get URL from content.

[\[06:01\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=361s) Okay, so if we just try to write a simple test for this, what's going to happen is that we're just going to get through all these dependencies and nothing's going to work. So how do we make this class more testable? Well, to get rid of the whole idea of the static so we can extract an override, we can do what's called a a static forwarder. We can keep the static method here,

[\[06:35\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=395s) But just a signature. So, what I'm going to do is I'm going to actually create a copy of this cl this method, duplicate it, and have the static removed. and call it send non-static for example. But the problem is that the compiler will say but you're in a static class. So I'm actually going to remove the word static here

[\[07:06\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=426s) To make things more testable. The only difference between when I have the word static or not is that the compiler will shout at me if I have non-static methods. So it's kind of useless for us. Now that I can have a non-static method, I'm going to go to the original static method and say just create an instance of new manager and call send non-static with the exact items.

[\[07:38\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=458s) So it's a static method that all all that it does is just forwarding the information. And the reason I need the static method is because I don't want to break all the existing callers to the method. Okay, this way the refactoring doesn't break the whole system, but it does allow me to now actually have a manager class that has instance methods. And now I can go to all the private static methods here. What's a private static method?

[\[08:09\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=489s) It means it's only going to get used by this class. So it doesn't really matter if it's static or not. And I can make them even protected and virtual. And I can then go and say, okay, I want to break something. Let's go to blog settings, enable trackback send. And instead of actually using a file, I can just extract a method from it and says

[\[08:44\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=524s) Enable trackback send and don't make it static. You please make it virtual. And that's it. Now in my test I can actually create a testable manager class that overrides this method and returns whatever we want without using a blog settings configuration file. And the same I can do from anything here. For example, get URLs from content.

[\[09:15\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=555s) What does that even mean? It means it goes and it downloads the actual URLs from links inside my content. If I did that, then I'm relying on the web to be present in my tests. It will become an integration test. So I can override this and return whatever I want. The same here like this whole thing remote file. I don't want to have a remote file whatever that means like read from web.

[\[09:46\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=586s) Get track back your from page parses using regular expressions. If a regular expression is too complicated to generate inside a test so I will easily find something then maybe it's a dependency as well and finally I can just extract and override these things to extract this into a virtual method etc. So again what I did to make this testable is I removed the static

[\[10:16\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=616s) And I made I took the existing static method created a copy of it which is non-static gave it a different name and made the static method just forward to a new instance of my class and called the non-static method. Yes. But in a reality or in a real life case we if we don't have any tests in a legacy code we would rather write some integuation test first before we do this. Yes. In the real world what you would do before you would do this because this is really risky is that you would have at least one or two integration tests that really use the web page and everything to make sure that the existing flow doesn't break and then you can refactor.

[\[10:59\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=659s) I wouldn't recommend this kind of refactoring without any integration test but it's worth it. An example is that one time I was sitting with a person I was consulting for them. They had a big legacy system written in C++ and they wanted to add a new attribute to the configuration file. Okay. But they we wanted to do it in a [test-driven](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-1#sigil_toc_id_27 "Test-driven development (The Art of Unit Testing, 3rd ed., ch. 1)") way. So what we did is first we wrote an integration test that actually loads the entire system in memory and with the old configuration file and we checked that the settings were loaded.

[\[11:33\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=693s) And then we wrote another test that loads them and then checks for the miss the new setting that was not there yet. And then we made it pass with the attribute. So we actually did a test-driven integration test. It was still worth it. >> >> other questions? Okay. So, what I want you to do is I is as an exercise, I want you to write a couple of tests or more that prove that trackback send gets sent

[\[12:10\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=730s) When you have at least one URL that in the content of a post. it get you want to prove also that it gets sent it gets called twice if you have two right because basically let's see there's a loop here so you want to make sure this loop actually works so you want to prove that if you have just one it gets called once if you have two it gets called twice and you also want to prove that if the configuration says that you should not enable track back then you should send a ping back.

[\[12:49\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=769s) Okay. You can also prove that if the track back URL is null because there is no trackback URL then it should send a ping back. All these things have a reason. So we should test them. Okay. Okay, so the refactoring we saw was create a static forwarder and then you can use extract and override. Okay, good luck.

[\[13:20\]](https://www.youtube.com/watch?v=Ae2OzBGvl7I&t=800s) If there are any questions, just let me know. >> No, you don't have to write the integration test for now. Just write the unit test as if you have integration.

## Related

- [Writing integration tests before refactoring](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-12#sigil_toc_id_225) · The Art of Unit Testing, 3rd ed., ch. 12

    The 3rd edition's section on writing integration tests before refactoring, needed before turning the static class into an instance.

- [Design goals for testability](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-11#ch11lev1sec2) · The Art of Unit Testing, 2nd ed., ch. 11

    The 2nd edition's design goals for testability, relevant to the static class and singleton that block extract and override.

- [Tips for Testable code and for testing legacy code](https://osherove.com/blog/2007/6/10/tips-for-testable-code-and-for-testing-legacy-code.html) · osherove.com

    Roy's consulting notes on getting a hard-to-test business method in legacy code under test.

- [Faking static methods in Moq, FakeItEasy and NSubstitute might be closer than imagined](https://osherove.com/blog/2012/7/8/faking-static-methods-in-moq-fakeiteasy-and-nsubstitute-migh.html) · osherove.com

    On faking static methods directly with proxy frameworks, an alternative to forwarding the static call to an instance.

### Also linked in this lesson

#### The Art of Unit Testing, 3rd ed.

- [Partial mocks](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-4#sigil_toc_id_95), ch. 4
- [Defining unit testing, step by step](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-1#sigil_toc_id_17), ch. 1
- [Working with legacy code](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-12), ch. 12
- [Integration tests](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-1#sigil_toc_id_25), ch. 1
- [Exit point types](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-1#sigil_toc_id_19), ch. 1
- [Test-driven development](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-1#sigil_toc_id_27), ch. 1

#### The Art of Unit Testing, 2nd ed.

- [Variations on refactoring techniques](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-3#ch03lev1sec5), ch. 3
- [Defining unit testing, step by step](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-1#ch01lev1sec1), ch. 1

#### osherove.com

- [Minimizing unit Test Fragility – 8 features in Typemock Isolator to help](https://osherove.com/blog/2009/7/9/minimizing-unit-test-fragility-8-features-in-typemock-isolat.html)
- [Unit Testing Entry and Exit Points](https://osherove.com/blog/2020/3/12/unit-testing-entry-points-and-exit-points)
- [Mock Objects vs. "Inherit & Override"](https://osherove.com/blog/2005/6/16/mock-objects-vs-quotinherit-amp-overridequot.html)

[Previous lesson Refactoring Hardcore Skills](https://courses.osherove.com/courses/legacy-code-hero/refactoring-hardcore-skills/) [Next lesson Extract And Override Focus](https://courses.osherove.com/courses/legacy-code-hero/extract-and-override-focus/)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
