I have a file contain multi function and I try to write unit-test for specific function using busted framework
code.lua:
function myfunc1(a,b)
-- do someting
return c1
end
function myfunc2(a2,b2)
-- do someting
return c2
end
code_spec.lua:
describe("function test", function ()
it("test result is the same in myfunc1", function ()
local functions = require "code"
local result = functions.myfunc1(500, 200)
assert.are.same(300, result)
end)
end)
but I get error like this
code_spec.lua:4: attempt to index a boolean value (local 'functions')
I need my unit test to evaluate output for specific input in myfunc1
or myfunc2
while my code and tests are in separated files.
I test different approaches, but documentation is a little confusing or lack of good example.
CodePudding user response:
Your code.lua
pollutes the global namespace rather than returning its exports. To fix your problem, you can either adjust your test to work around that problem, or you can fix that problem. To work around the problem, you'd call myfunc1
from the global namespace, like this:
require "code"
local result = myfunc1(500, 200)
To fix the problem, you'd instead return an export table from code.lua
, like this:
local p = {}
function p.myfunc1(a,b)
-- do someting
return c1
end
function p.myfunc2(a2,b2)
-- do someting
return c2
end
return p