CodePark/ questions / TDD & test design
START PRACTICINGPRACTICE
Backend question · Backend testing › TDD & test design

What is the Arrange-Act-Assert pattern, and what problems does it help you spot?

it('gives 10% off orders over 100', () => {
  // Arrange
  const order = anOrder().withTotal(150).build();
  const pricing = new PricingService();

  // Act
  const total = pricing.finalTotal(order);

  // Assert
  expect(total).toBe(135);
});

AAA (Arrange-Act-Assert) splits each test into three visible parts:

  • Arrange sets up the inputs, the test doubles (fakes, stubs and mocks) and the state.
  • Act runs the single behaviour under test. This is usually one call.
  • Assert checks the outcome.

This structure makes tests easy to read. You see what matters for this case and what is being tested.

It also shows problems:

  • A huge Arrange part suggests the code has too many collaborators (other objects it depends on). Or you may need a builder, a helper that creates test data.
  • Several Act steps usually mean the test covers more than one behaviour. Split it.
  • Assertions mixed into the Arrange part make it unclear what failed.

Given-When-Then from BDD (behaviour-driven development) is the same idea in other words. Blank lines are enough to separate the three parts. Comments like // Arrange are optional.

  • Arrange the inputs and state, Act once, then Assert the outcome.
  • AAA makes a test readable: it shows what matters for the case.
  • A big Arrange part hints at a design problem or a missing test-data builder.
  • Several Act steps mean the test should be split.