Writing tests for pure functions

Session Outline

Connect: Pure or not quizz

Examine the code snippets. Note down which are pure functions.

Concrete: Trig math

Use the cyber-dojo starting point.

Reflect

Fist to five vote: 5 fingers = very, zero = not at all

Ask for comments: how did this feel?

Concept: Pure functions are easier to test

This code was relatively easy to get basic tests in place. Mostly small functions, but (more importantly) pure functions

An easy-to-test function has these properties:

  1. You can see all the inputs in the argument list
  2. The execution is deterministic (same input produces same output)
  3. You can see all the outputs in the return value

Code that is harder to test will lack some or all of these properties.

Reflect: What about your code?

Fist to five vote: 5 fingers = very, zero = not at all