Dallonses logo

Mocking

What is mocking?

Mocking replaces a real dependency with a fake one so a test can run in isolation. When the code you're checking calls a payment provider, a database, or a third-party API, you don't want the test to actually charge a card or depend on a server being up. A mock stands in for that dependency, returns the response you tell it to, and lets you verify the code in question without dragging the whole world into the test.

The vocabulary around it is precise, and the distinctions matter. A stub returns a canned answer so the code has something to work with. A mock goes further and records how it was called, so the test can assert that a function fired with the right arguments the right number of times. A fake is a lightweight working implementation, like an in-memory database that behaves like the real one but lives only for the test. Mocking when a checkout calls a payment API means the test can simulate a successful charge, a declined card, and a timeout in three separate runs, none of which touch real money.

The trade-off is realism. A mock only behaves the way you told it to, so if your assumption about the real dependency is wrong, the test passes while production fails. That's why mocks belong in unit tests, where isolation is the point, and integration tests still exercise the real connections to confirm the assumptions hold.

Mocking at Dallonses

We mock to make tests fast and deterministic, and we know where to stop. Unit tests mock the external world so they run in milliseconds and never flake on a network hiccup. The integration layer keeps the real connections, because that's the only place where a wrong assumption about a third-party API actually gets caught.

The risk we watch for is a suite that's green because it's only testing the mocks. Our automated testing keeps mocks honest by pinning them to the real contracts and updating them when an API changes, so a passing test means working code rather than a comfortable fiction. We document what's faked and why, so the team that inherits the suite knows exactly what it does and doesn't prove.

Tests slow, flaky, or hitting real services they shouldn't? Let's tighten them up.

Talk to us about QA

Related services


Ready to work together?

Book a meeting
Aymón holding a Tools magazine in front of their facem
Ari working on a laptop outdoors surrounded by plants
Top-down view of a wooden desk with a keyboard, mouse, and headphones
Hand-drawn illustration of a hand snapping fingers
Nico leaning against a water cooler next to a fire extinguishe
Close-up of an open computer with circuit board and components on a wooden desk
Bernat and Andreu collaborating at a desk with monitors and a laptop
Hand-drawn illustration of an open hand waving
Aymón holding a Tools magazine in front of their facem
Ari working on a laptop outdoors surrounded by plants
Top-down view of a wooden desk with a keyboard, mouse, and headphones
Hand-drawn illustration of a hand snapping fingers
Nico leaning against a water cooler next to a fire extinguishe
Close-up of an open computer with circuit board and components on a wooden desk
Bernat and Andreu collaborating at a desk with monitors and a laptop
Hand-drawn illustration of an open hand waving