I am trying to unit test code using Busted (which I did not write, nor am I allowed to refactor at this time for business reasons) in Lua, and there is no concept in this module of classes nor dependency injection. So, I'd like to replace some of the modules required at the top of the file, i.e local log = require("path.to.module.logger"):new()
with a mocked logger that I made to track the number of times methods are called, i.e logger:trace()
such as with times()
in Mockito in Java. In Java, I can use Reflection.Utils for this purpose. What's the equivalent in Lua to help make this not-testable code, testable?
I've already tried creating a global variable with the same variable name log
and setting it equal to my mock using this example: https://www.lua.org/pil/14.2.html
local _M = {}
local log = require("path.to.module.logger"):new()
...
function _M.init(...) log:trace("debug") # I would like this log instance to not be the one above, rather the one I inject into the module at runtime end