What is a test stub?

_config.yml

(Just a really cool picture of a transmission system I shamelessly copied off the internet :P)

What is a test stub?

Let’s start with the formal definition Manning uses - please bear with me if that means nothing to you.:

A stub is a controllable replacement for an existing dependency (or collaborator) in the system. By using a stub, you can test your code without dealing with the dependency directly. – The Art of Unit Testing (Manning)

Yeah, but what does it actually mean?

Like most things, this is best illustrated by example:

Let us suppose you are building a car, moreover, Henry Ford has assigned to you the task of building the car’s transmission. A transmission system basically transfers power from an engine to the car’s wheels. In other words, if you have a transmission system, you will also have wheels, and you will also have an engine.

_config.yml

  1. Question 1: Do you need a working engine in order to build and design a transmission system?

Ordinarily having an engine is very handy. But the problem is that the engine design team haven’t yet designed an engine! You don’t want to wait for them to design it and to then start designing the transmission. This happens all the time in real life scenarios: if teams waited for each other then it would take an infinity to design anything! So then the design of the transmission proceeds on the very rough assumption that an engine is going to transfer energy through it to the wheels. Perhaps a rough idea of the connecting details needs to be nutted out too.

So then how do you proceed to build and test the transmission system?

You don’t have an engine by which you can test your transmission system. So what do you do? You get someone in your team to create a substitute for the engine!

Now the substitute need not be complicated. It can’t be. It can be as simple as someone powering the transmission with a crank – simulating an engine – or perhaps with a wheel, or a spanner, or even his bare hands! And on the other end you need a wheel. What if you don’t have a wheel? Then you make a substitute wheel made of, for example, paper Mache.

Summary of above points:

Basically we now have a:

  1. Fake engine which applies power to our:

  2. genuine transmission system which we have built, which transfers power to our:

  3. Fake wheels.

Voila: we’ve built and tested something without it’s essential dependencies. (If you don’t know what that means please checkout my post on the subject). In other words, we’ve built a transmission system, without the essential ingredients required for a propery working transmission: a real engine and real wheels.

Definition of a stub:

  • In the above example, the fake engine, cranked by hand, is actually a test stub. The fake wheels, made of paper Mache is also a wheel test stub. They are meant to imitate the real thing, but without the complexity. That is what a stub is.

Using this system, we can test to a very high degree of certainty whether our transmission system is actually working. We can build it without an engine! That is a huge advantage of using this type of procedure.

Further advantages of using fake engines and fake transmissions: Imagine if we actually used a real engine, real transmission and real wheels. All the parts are genuine: but let’s suppose when we try to start the car: and it does not work! Now my million dollar question to you is: where does the problem lie?

It could be in any one or more of the following:

  1. The real engine – who’s job is to apply power to the:

  2. Real transmission who’s job is to transfer energy to the:

  3. Real wheels – which cause the car to move.

How will you track down the error?

Given you already have tested the transmission using stubs, you know for sure that the problem is not in the transmission! Eliminating one source of error is huge! The problem’s gotta lie in either the engine, or in the wheels!

I’m assuming that you can similarly test those things using stubs – and if the stub fails, then you know exactly where the problem is.

That my friend is what a stub is, and why it is so very useful:

Overall summary

  1. It allows you to isolate and test for any potential problems.

  2. It allows you to building something without needing, in reality, other essential components.

Written on December 8, 2016