Skip to main content

Module doubles

Module doubles 

Source
Expand description

Spy / stub / mock test doubles implemented as Lua UserData.

Provides register to inject a test_doubles global table into a Lua VM with the following factory functions:

-- spy: transparently records calls while delegating to the original
local s = test_doubles.spy(function(x) return x * 2 end)
s(5)
assert(s:call_count() == 1)
assert(s:was_called_with(5))

-- stub: returns fixed values without calling any original
local st = test_doubles.stub()
st:returns(42)
assert(st() == 42)

-- spy_on: replaces a table method with a spy, supports revert()
local obj = { greet = function(name) return "hello " .. name end }
local s = test_doubles.spy_on(obj, "greet")
obj.greet("world")
assert(s:call_count() == 1)
s:revert()  -- restore the original method

Functions§

register
Register the test_doubles global table into the given Lua VM.