Testing the Resources Layer: Mocks, Containers and Contracts

The tests for your Resources code all pass, the deploy goes out, and checkout fails because the carrier quietly renamed a field. Everything you tested was your own understanding of the carrier. The Resources layer is awkward to test because it is the code that meets slow, stateful, unpredictable systems, and one kind of test cannot cover that. The book uses three.

Three questions, three kinds of test

Each technique answers a different question about a repository or adapter.

  • Do we translate correctly? Unit tests with mocks or stubs standing in for the database, cache or HTTP client. They check that rows map to domain objects, that vendor errors become domain errors, and that the cache behaves as intended. They are fast and deterministic, but a mock proves nothing about real connectivity.
  • Does it work against the real thing? Integration tests with Testcontainers, which start a Dockerised database for the test run and tear it down when the suite ends. Your SQL meets an actual engine instead of an imitation.
  • Do both sides still agree? Contract tests, with a tool such as Pact. The consumer's expectations are written down as a contract and verified against the provider. The book finds this most valuable when the API belongs to another team or organisation.

The first catches your mistakes, the second catches the database's opinions, and the third catches the other side changing. Skip one and that category of failure waits for production.

The mock-based tests are the ones you will write most often, so give them the layer's characteristic scenarios: a cache hit that should skip the database, a duplicate-key error that should become a domain conflict, and a vendor timeout that should be marked transient.

A realistic suite uses all three, in that order of quantity. Mock-based tests are numerous because they are cheap. Container tests are fewer because they start real infrastructure. Contract tests are fewer still, but they guard the seam where you have the least control.

A small example

Retry behaviour is a good candidate for a unit test, provided time is injected so the test does not wait:

it("retries a 503 and then succeeds", async () => {
  const carrier = fakeCarrier([http(503), http(503), ok({ code: "IN_TRANSIT" })]);
  const tracking = new TrackingResource(carrier, { sleep: async () => {} });

  const result = await tracking.statusFor("TRK-1");

  expect(result.status).toBe("in_transit");
  expect(carrier.calls).toBe(3);
});

Three details make this useful. The fake carrier is scripted, so the failure is repeatable. The sleep function is replaced, so the test takes milliseconds. And the assertion is about the domain value, in_transit, rather than the vendor's code, which checks translation and retry together.

How much is enough

The book sets the coverage target for Resources at 80% or higher, lower than for the bottom layers, and gives the reason. Some failure paths, such as network partitions and TLS handshake failures, are hard to reproduce deterministically, so chasing every line would waste effort. It then names what must not be skimped on: mapping logic, error translation, caching behaviour, and the state transitions of retry and circuit-breaker code.

There is a reason the number is lower here than in the foundation layers. Utils and Schema contain no I/O, so every line can be exercised with ease. Resources code is dominated by interactions with the outside world, and some of those interactions simply cannot be staged in a test run.

That list is a handy checklist. Whatever the percentage says, if a mapping function or a breaker transition has no test, the number is hiding a real gap.

The takeaway

Match the test to the question, and spend your coverage where the layer does its real work: translation, errors, caching and resilience. For the code under test, see resilience patterns and caching and error translation. The wider picture of how tests are shaped across layers is in the LVD testing pyramid.

What the book covers

  • Mocking techniques for repositories and adapters, including cache and database doubles
  • Running a real PostgreSQL instance in a container for integration tests
  • Consumer-driven contract testing with Pact for APIs owned by other teams
  • Why the coverage target is 80% and which behaviours must be tested thoroughly