Mocks and Spies
Isolate code under test.
Mocks and Spies is a free Lua Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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()Spying on a Method
Use spy.on to watch a method on an existing table. The original still executes, but busted tracks each call.
This is ideal for confirming a side effect, such as a logger being called.
spy.on(logger, "info")
service.run()
assert.spy(logger.info).was.called()Checking Call Counts
Spies record how many times they were called. Use was.called(n) to assert an exact count.
This catches bugs where a function fires too often or not at all.
assert.spy(s).was.called(2)
assert.spy(s).was_not.called()Checking Call Arguments
You can assert which arguments a spy received using was.called_with. busted compares the recorded arguments against your expectation.
This verifies not just that a function ran, but that it ran correctly.
local s = spy.new(function() end)
s("user", 42)
assert.spy(s).was.called_with("user", 42)What a Stub Does
A stub replaces a function entirely so the original never runs. Use it to skip slow or undesirable work, like real network or disk access.
Create one with stub on a table field. By default the stub does nothing and returns nil.
stub(api, "fetch")
service.load()
assert.stub(api.fetch).was.called()Stub Return Values
You can make a stub return a fixed value with returns. This feeds controlled data into the code under test.
Now your test does not depend on the real dependency at all.
stub(api, "fetch").returns({id = 1})
local r = service.load()
assert.are.same({id = 1}, r)What a Mock Does
A mock takes an existing table and turns all of its functions into spies or stubs at once. It is a convenient way to track a whole object.
Call mock(obj) to wrap it, then assert on individual methods.
local m = mock(realLogger)
app.start(m)
assert.spy(m.warn).was.called()Mock with Stubbed Methods
Passing true as the second argument to mock stubs every method, so none of the originals run. This fully isolates the object.
Use this when the real methods have side effects you must avoid.
local db = mock(database, true)
db.query.returns({})
assert.are.same({}, repo.all())Restoring Doubles
Spies, stubs, and mocks alter shared tables, so you must restore them or later tests will see the double. Call :revert() or mock.revert.
Place restoration in after_each to guarantee clean state between tests.
after_each(function()
mock.revert(m)
end)Choosing the Right Double
Use a spy to observe a real function, a stub to replace one with controlled behavior, and a mock to wrap a whole object at once.
Prefer the lightest double that does the job. Over mocking can make tests brittle and hide real bugs.
describe("mailer", function()
it("sends once", function()
local s = spy.on(smtp, "send")
mailer.notify("a@b.com")
assert.spy(s).was.called(1)
end)
end)Quick Check
Test your understanding of busted test doubles.
Recap
Test doubles isolate code from slow or external dependencies. A spy records calls while running the original, a stub replaces a function with controlled behavior, and a mock wraps a whole object.
Assert with was.called and was.called_with, and always restore doubles with revert in after_each to keep tests isolated.
Frequently asked questions
Is the “Mocks and Spies” lesson free?
Yes — the full text of “Mocks and Spies” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Mocks and Spies”?
Isolate code under test. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Mocks and Spies” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Test
- describe and it Blocks
- Assertions
- Mocks and Spies