lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


In python's Mock module, there's autospeccing thing, you can let the mock object
behave exactly like the object you mocked, like you should have the same args signature.
I don't know if there's the same thing in lemock.
Now take such situation.


public.lua
---------------------------------------------------
local M = {}
function M.add_internal(a, b)
    return a+b
    --add_internal may call some global function, so I should mock add_internal
end
function M.add(a, b)
    return M.add_internal(a, b)
end
return M

Then I need to test public.add function, because add_internal may could call some global function,
I'd like to mock add_internal function.


test.lua
---------------------------------------------------
require("lunit")
lunit.setprivfenv()
lunit.import "assertions"
lunit.import "checks"

require("lemock")


local ut = lunit.TestCase("public.add function test")
function ut:test()
    local public   = require("public")
    mc = lemock.controller()
    mock_add_internal    = mc:mock()
    package.loaded.public.add_internal = mock_add_internal
    mock_add_internal(2, 3); mc:returns(5)
    mc:replay()
    local result = public.add(2, 3)
    assert_equal(5, result)
    
    mc:verify()
end
lunit.run()


Now take such situation, I modify public.add_internal'code as below:
public.lua
---------------------------------------------------
local M = {}
function M.add_internal(a, b, c)
    return a+b+c
    --add_internal may call some global function, so I should mock add_internal
end

function M.add(a, b)
    return M.add_internal(a, b)
end
return M

But I leave the test.lua unmodified, the unittest would pass too.
Is there a way to solve this problem?