2013-03-13 21 views
0

我想运行一个redis lua模拟项目来测试我的redis lua代码。但显然,redis-mock项目中存在缺陷。如何调用变量参数方法与解压

当我在我的测试代码中调用redis.call('hget', 'foo', 'bar'),Redis的嘲笑在hash.lua#22抛出断言错误是由RedisLua.lua#20

-- RedisLua.lua 
local call = function(self) 
    return (function(cmd, ...) 
    cmd = string.lower(cmd) 

    local arg = {...} 

    local ret = self.db[cmd](self.db, unpack(arg)) -- line 20 

    if self.RedisLua_VERBOSE then 
     print(cmd .. "(" .. table.concat(arg, " ") .. ") === ".. tostring(ret)) 
    end 

    return ret 
    end) 
end 


-- hash.lua 
function RedisDb:hget(self,k,k2) 
    assert((type(k2) == "string")) -- # line 22 
    local x = RedisDb.xgetr(self,k,"hash") 
    return x[k2] 
end 

跟踪后打电话,我找到了,self'foo',该k'bar'和该k2实际上是nil,我怎样才能修复这个bug,该kfoo,和k2'bar'

回答

1

我认为你需要拨打redis:call('hget', 'foo', 'bar')或等效redis.call(redis,'hget','foo','bar'),而不是redis.call('hget', 'foo', 'bar')

+0

无需想着那里。 :P – hjpotter92 2013-03-13 07:32:35

+0

这是一个模拟或Redis,其中Redis官方定义'redis.call(cmd,...)',如果模拟不遵循官方API,那么它是没有意义的。 – 2013-03-13 08:44:37

0

回答我自己的问题。

定义为:时,不需要self

-- hash.lua 
function RedisDb:hget(self,k,k2) 
    assert((type(k2) == "string")) -- # line 22 
    local x = RedisDb.xgetr(self,k,"hash") 
    return x[k2] 
end 

变化

-- hash.lua 
function RedisDb:hget(k,k2) 
    assert((type(k2) == "string")) -- # line 22 
    local x = RedisDb:xgetr(k,"hash") 
    return x[k2] 
end