Roll Your Own Mock Framework

Level: Advanced 60–90 min

Concepts: MockingDesign PatternsBoundariesIncremental Design


Build a basic mock object framework that lets developers create test doubles, set up return values, and verify method calls. Understanding how mocks work under the hood makes you better at using them.

Inspired by Jason Gorman’s Codemanship kata.

Requirements

Step 1: Create a Mock

Given an interface or class, create a mock instance that implements the same contract.

interface Calculator {
  add(a, b): number
  subtract(a, b): number
}

mock = createMock(Calculator)

The mock should accept any method call without throwing.

Step 2: Stub Return Values

Configure a mock to return specific values when methods are called with specific arguments.

when(mock.add(2, 3)).thenReturn(5)

result = mock.add(2, 3)  // returns 5
result = mock.add(1, 1)  // returns undefined/null (no stub configured)

Step 3: Verify Invocations

After exercising the system under test, verify that expected methods were called.

mock.add(2, 3)

verify(mock.add).wasCalledWith(2, 3)  // passes
verify(mock.add).wasCalledWith(1, 1)  // fails with meaningful message
verify(mock.subtract).wasCalled()      // fails — never called

Step 4: Meaningful Failure Messages

When verification fails, report what was expected vs what actually happened:

Expected: add(1, 1) to be called
Actual: add was called with (2, 3)

Test Cases

ScenarioExpected
Create mock from interfaceMock instance created, methods callable
Call unstubbed methodReturns null/undefined, no error
Stub return valueReturns configured value for matching args
Stub with different argsEach arg set returns its own value
Verify called methodPasses
Verify uncalled methodFails with message
Verify wrong argumentsFails with expected vs actual message
Verify call countwasCalledTimes(2) passes if called twice

Bonus

  • Support wasCalledTimes(n) — verify exact call count
  • Support wasNeverCalled() — verify a method was not invoked
  • Support argument matchers — verify(mock.add).wasCalledWith(anyNumber(), 3)
  • Support thenThrow(error) — mock throws instead of returning
  • Support call ordering — verify method A was called before method B

Hint

The core mechanic is a proxy that records all method calls (name + arguments) in a list. Stubbing adds entries to a return-value lookup. Verification searches the recorded calls. Start with recording calls and verifying them — add stubbing after.