0Pricing
Lua Academy · Lesson

Mocks and Spies

Isolate code under test.

Test Doubles

Sometimes a function depends on something slow or external, like a network call or a logger. Test doubles replace those dependencies with controllable stand ins.

busted provides three kinds: spies, stubs, and mocks. Each serves a different need.

What a Spy Does

A spy wraps a function and records how it is called while still running the original. It lets you verify that something was invoked.

Create one with spy.new around a function, or spy on a table field.

local s = spy.new(function() end)
s(1, 2)
assert.spy(s).was.called()

All lessons in this course

  1. Why Test
  2. describe and it Blocks
  3. Assertions
  4. Mocks and Spies
← Back to Lua Academy