【发布时间】:2014-04-30 01:11:03
【问题描述】:
我正在尝试使用 busted 测试我们的 Freeswitch lua 脚本,但遇到了障碍。它的要点是我需要能够监视如下代码
local req_host = session:getVariable('sip_req_host')
session:setVariable('curl_timeout', 0)
但我似乎无法弄清楚如何构建我应该将 _G.session 设置为的对象。我能找到的关于如何使用 busted 的最佳/唯一好的示例是 https://github.com/chris-allnutt/unit-tested-corona/blob/master/mocks/button.lua,但它似乎使用与 busted 文档相同的简单语法来构建模拟对象。
local button = {
x = 0,
y = 0,
addEventListener = function() end
}
我可以看到这对于不需要返回任何内容但我需要能够使用 getVariable 和 setVariable 函数在会话对象中获取和设置变量的简单函数是如何工作的。我的简单模拟对象如下:
Session = {}
Session.__index = Session
function Session.create(params)
local session = {}
setmetatable(session, Session)
session.params = params
return session
end
function Session:getVariable(key)
return self.params[key]
end
function Session:setVariable(key, val)
self.params[key] = val
end
function Session:execute(cmd, code)
end
测试如下
require "busted"
require("test_utils")
describe("Test voip lua script", function()
it('Test webrtc bad domain', function()
domain = 'rtc.baddomain.com';
session_params = {['sip_req_host'] = domain,
['sip_req_user'] = 'TEST-WebRTC-Client',
["sip_from_user"] = 'testwebrtc_p_12345',
['sip_call_id'] = 'test@call_id',
['sip_authorized'] = 'false'}
exec_str = 'sofia_contact TEST-WebRTC-Client@'..domain;
api_params = {[exec_str] = 'error/user_not_registered'}
_G.session = mock(Session.create(session_params), 'execute')
_G.api = API.create(api_params)
_G.freeswitch = Freeswitch.create()
dofile("tested_script.lua")
assert.spy(_G.session.execute).called_with("respond", "407")
end)
end)
我最终遇到了以下异常。 /usr/local/share/lua/5.2/luassert/spy.lua:78: 尝试索引一个函数值
这个异常是被 luassert 抛出的,它是被破坏的库的一个依赖,在下面的 if 语句中
77:local function called_with(state, arguments)
78: if rawget(state, "payload") and rawget(state, "payload").called_with then
79: return state.payload:called_with(arguments)
80: else
81: error("'called_with' must be chained after 'spy(aspy)'")
82: end
83:end
我对 lua 很陌生,所以我似乎只是遗漏了该语言的一些明显部分,但任何帮助或指针将不胜感激。
【问题讨论】:
-
spy.lua 是带有最后一个代码 sn-p 的文件吗?如果是,第 78 行是哪一行?如果不是 spy.lua 的第 78 行是什么?
-
我编辑了我的帖子以添加相关行。整个文件位于github.com/Olivine-Labs/luassert/blob/master/src/spy.lua
标签: unit-testing lua freeswitch lua-busted