[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: lemock mock out another function along with SUT in the same module
- From: albert_200200 <albert_200200@...>
- Date: Tue, 22 Jul 2014 15:10:23 +0800
Hi, guys, I'm using lunit and lemock to test my code,
Here is the situation: the function I should mock is in the same module
of the function I'm testing.
Below is the code I write.
foo.lua
---------------------------------------------------------------------------------------------------------------
local M = {}
function M.dowork_internal(a, b)
return global_function(a, b)
end
function M.dowork(a, b)
return M.dowork_internal(a, b)
end
return M
test.lua
---------------------------------------------------------------------------------------------------------------
require("lunit")
lunit.setprivfenv()
lunit.import "assertions"
lunit.import "checks"
require("lemock")
local ut = lunit.TestCase("module test")
function ut:setup()
self.mc = lemock.controller()
self.mock_dowork_internal = self.mc:mock()
end
function ut:test_normal()
local vala = 1
local valb = 2
self.mock_dowork_internal(vala, valb); self.mc:returns(2)
self.mc:replay()
package.path = "./?.lua;"..package.path
local foo = require("foo")
package.loaded.foo.dowork_internal = nil
package.preload["foo.dowork_internal"] = function()
return self.mock_dowork_internal
end
local val = foo.dowork(vala, valb)
assert_equal(val, 2)
self.mc:verify()
end
lunit.run()
I'm stuck with it for days. I'd really appriciate if someone kind could
help. Thanks in advance.