Gunjan Doshi

Startups, Entrepreneurship, Agility, Management & Leadership, Metrics

Self Shunt Pattern

November 29th, 2004 by gunjandoshi

As mentioned in the TDD book, self-shunt is used to test that an object under test communicates correctly with its collaborator. If we are not using the self-shunt pattern, the test case would act as a intermediary between the object under test and its collaborator.

With self-shunt, we let the test case itself, disguise as a collaborator. The test case passes itself to the object under test, the object under test then interacts with the test case, and then the test case checks its own state. I will say, self-shunt is a specialized case of mock object pattern, where the test case itself acts as a mock.

Self-shunt can be a unmanageablegood starting point. However, self-shunt forces me to extract an interface from the collaborator, so that my test case can implement it. If the collaborator has several methods, it can be a pain to implement all the methods in the test case. In addition, it can make my test case unreadable. When the self-shunt becomes unmanagable, it becomes mandatory to refactor to a real mock object.

References:
1. TDD Book, Self Shunt. Pg. 145
2. Mike Feather’s article

Tags: No Comments

Leave a Comment

0 responses so far ↓

There are no comments yet...Kick things off by filling out the form below.