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
- Why Test
- describe and it Blocks
- Assertions
- Mocks and Spies