I'd like to create a simple mock table that would tell me what was tried to be called from it.
My first try was:
local function capture(table, key)
print("call to " .. tostring(table) .. " with key " .. tostring(key))
return key
end
function getMock()
mock = {}
mt = { __index = capture }
setmetatable(mock, mt)
return mock
end
Now calling this with
t = getMock()
t.foo
prints as I expected:
call to table: 002BB188 with key foo
but trying to call:
t.foo("bar")
gives:
call to table: 002BB188 with key foo
lua: test.lua:6: attempt to call field 'foo' (a string value)
Now I have two questions:
- How to avoid the exception, ie. what am I doing wrong?
- How to catch the method argument too ("bar" in this case)?