Agent Wranglers

TST-13Recommended

Fake only what you do not own

Replace only outside services, such as a payment provider or an email API, with test doubles. Run your own code as it runs in production.

How to do it

Put each outside service behind a small interface. In tests, pass a fake that records calls and returns set answers. Do not mock your own classes inside one feature, because the test then checks the mocks instead of the code. Code shaped as Depend on abstractions at the edges says makes these fakes easy to plug in.

Examples

Python example

class FakePayments:
    def __init__(self):
        self.charges = []

    def charge(self, amount, card):
        self.charges.append(amount)
        return "ch_test_1"

def test_checkout_charges_the_cart_total(db):
    payments = FakePayments()
    checkout(cart_with_total(42_00), payments=payments, db=db)
    assert payments.charges == [42_00]

TypeScript example

class FakePayments {
  charges: number[] = [];
  charge(amount: number) { this.charges.push(amount); return 'ch_test_1'; }
}

test('checkout charges the cart total', async () => {
  const payments = new FakePayments();
  await checkout(cartWithTotal(4200), { payments, db });
  expect(payments.charges).toEqual([4200]);
});

How to check

Search the integration tests for mocks of your own modules, such as vi.mock('../, jest.mock('./ or mock.patch("shop.. There are none. Doubles exist only for outside services.

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/testing/integration-and-contract-tests/doubles-at-the-edge">Agent Wranglers testing playbook</a>. In code you write, add a comment next to it with the same link.