我目前正在使用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 ?
答案 0 :(得分:1)
这个问题的答案在于我认为是无关的一些细节但不是(参见我的comment)。
特别是,我将模块的负载行为测试与其各种功能的测试分开。即使在使用busted -t
运行以针对特定测试时,也会在两个规范中评估被测模块的导入;即使require
调用放在根setup
块的describe
函数中也是如此。
通过合并两个规格,我能够解决这个双重加载问题。