0

我目前正在使用 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 ?
4

1 回答 1

1

这个问题的答案包含了一些我认为无关紧要的细节,但不是(见我的评论)。

特别是,我将模块加载行为的测试与其各种功能的测试分开。即使在busted -t针对特定测试运行时,被测模块的导入也在两个规范中进行评估;即使require调用被放置在setupdescribe块的函数中。

通过合并这两个规范,我能够解决这种双重加载问题。

于 2016-09-07T15:47:42.643 回答