Skip to content

l0.DependencyControl.Stub

Stub = require "l0.DependencyControl.Stub"
local Stub = require("l0.DependencyControl.Stub")

Stub

A callable stub that records invocations and supports fluent configuration and assertions. Can be used standalone or via UnitTest:stub for automatic lifecycle management.

Constructor

stub = Stub table, key, logger, unitTest
local stub = Stub(table, key, logger, unitTest)

Creates a stub, optionally replacing a key in a table.

param type description
table? table|string The table to stub into, or a module name (looked up in the module cache).
key? string The field name to replace; no table is modified when nil.
logger? Logger Logger to use; a default logger is used when nil.
unitTest? UnitTest Unit test instance to report assertion failures to; failures throw when nil.

Instance methods

calls

stub\calls impl
stub:calls(impl)

Sets the function to invoke when the stub is called.

param type description
impl function

Returns:

returns

stub\returns ...
stub:returns(...)

Sets the stub to return fixed values on every call.

param type description
... any Values to return from every call.

Returns:

restore

stub\restore!
stub:restore()

Restores the original value that was replaced by this stub.

assertCalled

stub\assertCalled!
stub:assertCalled()

Asserts the stub was called at least once.

assertNotCalled

stub\assertNotCalled!
stub:assertNotCalled()

Asserts the stub was never called.

assertCalledTimes

stub\assertCalledTimes n
stub:assertCalledTimes(n)

Asserts the stub was called exactly the given number of times.

param type description
n integer The expected call count.

assertCalledOnce

stub\assertCalledOnce!
stub:assertCalledOnce()

Asserts the stub was called exactly once.

assertCalledOnceWith

stub\assertCalledOnceWith ...
stub:assertCalledOnceWith(...)

Asserts the stub was called exactly once and with the given arguments.

param type description
... any The arguments expected on the single call.

assertCalledWith

stub\assertCalledWith ...
stub:assertCalledWith(...)

Asserts at least one recorded call was made with the given arguments.

param type description
... any The arguments expected on some call.

assertLastCalledWith

stub\assertLastCalledWith ...
stub:assertLastCalledWith(...)

Asserts the most recent call was made with the given arguments.

param type description
... any The arguments expected on the last call.

assertNthCalledWith

stub\assertNthCalledWith n, ...
stub:assertNthCalledWith(n, ...)

Asserts the nth recorded call was made with the given arguments.

param type description
n integer The 1-based index of the call to check.
... any The arguments expected on that call.

Class methods

spy

Stub\spy table, key, logger, unitTest
Stub:spy(table, key, logger, unitTest)

Creates a spy on a method, recording calls while still invoking the original method.

param type description
table table|string The table to spy into, or a module name (looked up in the module cache).
key string The field name to spy on.
logger? Logger Logger to use; a default logger is used when nil.
unitTest? UnitTest Unit test instance used to report assertion failures.

Returns: