我目前正在使用 Busted 为 lua mod 库编写单元测试。有问题的文件定义了一个带有一些函数的模块,然后在底部调用其中一个函数来初始化自身。
我发现的问题是 Busted 似乎正在两次评估所需的文件。
测试
it('does a thing', function()
-- Some setup, replacing globals etc
require('items')
assert.are_equal(2, #Items._registry)
end)
模块
Items = { _registry = {} }
function Items.do_some_stuff() end
function some_util_func() end
function load_registry()
print(debug.traceback())
for i, itm in pairs(Items.do_some_stuff()) do
Items._registry[i] = itm
end
end
load_registry()
如您所见,虽然我已经简化了代码和名称,但结构并不是出乎意料的(据我所知)。
测试将始终失败,因为#Items._registry
始终为 0(并且转储到控制台验证了这一点)。我尝试在方法内部打印,发现它打印了两次;然后我尝试debug.traceback
在该功能的顶部使用并找到以下内容。如您所见,堆栈回溯打印了两次,表明代码被评估了两次。
这是其他人遇到过的吗?对于这种情况,我的测试结构是否错误?或者这是一个错误?
stack traceback:
items.lua:96: in function 'load_registry'
items.lua:109: in main chunk
[C]: in function 'require'
spec/items_pec.lua:50: in function <spec/items_spec.lua:39>
[C]: in function 'xpcall'
/usr/local/share/lua/5.2/busted/core.lua:178: in function 'safe'
/usr/local/share/lua/5.2/busted/init.lua:40: in function 'executor'
/usr/local/share/lua/5.2/busted/core.lua:312: in function </usr/local/share/lua/5.2/busted/core.lua:312>
[C]: in function 'xpcall'
/usr/local/share/lua/5.2/busted/core.lua:178: in function 'safe'
...
/usr/local/share/lua/5.2/busted/core.lua:312: in function 'execute'
/usr/local/share/lua/5.2/busted/block.lua:155: in function 'execute'
/usr/local/share/lua/5.2/busted/init.lua:7: in function 'executor'
/usr/local/share/lua/5.2/busted/core.lua:312: in function </usr/local/share/lua/5.2/busted/core.lua:312>
[C]: in function 'xpcall'
/usr/local/share/lua/5.2/busted/core.lua:178: in function 'safe'
/usr/local/share/lua/5.2/busted/core.lua:312: in function 'execute'
/usr/local/share/lua/5.2/busted/execute.lua:58: in function 'execute'
/usr/local/share/lua/5.2/busted/runner.lua:174: in function </usr/local/share/lua/5.2/busted/runner.lua:11>
/usr/local/lib/luarocks/rocks/busted/2.0.rc12-1/bin/busted:3: in main chunk
[C]: in ?
stack traceback:
items.lua:96: in function 'load_registry'
items.lua:109: in main chunk
[C]: in function 'require'
spec/items_spec.lua:15: in main chunk
[C]: in function 'xpcall'
/usr/local/share/lua/5.2/busted/core.lua:178: in function 'safe'
/usr/local/share/lua/5.2/busted/block.lua:146: in function 'execute'
/usr/local/share/lua/5.2/busted/init.lua:7: in function 'executor'
/usr/local/share/lua/5.2/busted/core.lua:312: in function </usr/local/share/lua/5.2/busted/core.lua:312>
[C]: in function 'xpcall'
/usr/local/share/lua/5.2/busted/core.lua:178: in function 'safe'
/usr/local/share/lua/5.2/busted/core.lua:312: in function 'execute'
/usr/local/share/lua/5.2/busted/execute.lua:58: in function 'execute'
/usr/local/share/lua/5.2/busted/runner.lua:174: in function </usr/local/share/lua/5.2/busted/runner.lua:11>
/usr/local/lib/luarocks/rocks/busted/2.0.rc12-1/bin/busted:3: in main chunk
[C]: in ?