lua-users home
lua-l archive

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


On Tuesday, July 22, 2014 03:10:23 PM albert_200200 wrote:
> 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.

I'm not entirely familiar with lemock, however...

>      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)

That isn't how package.preload works. The preload functions are module 
loaders. The loader function is called by require to create the module table. 
The value returned by the loader is then stored in package.loaded.foo and also 
returned by require.

I think all you need is to assign the mock function to foo.dowork_internal.

-- 
tom <telliamed@whoopdedo.org>