Lua语言中是否有类似于Ruby WebMock的工具?

我正在编写一个 Lua 模块,用于向公共 API 发送请求:

-- users.lua

local http     = require("socket.http")
local base_url = 'http://example.com'
local api_key  = "secret"
local users    = {}

function users.info(user_id)
  local request_url = base_url .. '/users/' .. user_id .. "?api_key=" .. api_key
  print("Requesting " .. request_url)
  local response = http.request(request_url)
  print("Response " .. response)
  return response
end

return users

虽然这可以工作,但我想使用 TDD 完整编写 API 包装器。

我有一个规范(使用busted框架),可以正常工作,但它实际上会向 API 发出请求:

-- spec/users_spec.lua

package.path = "../?.lua;" .. package.path

describe("Users", function()
  it("should fetch the users info", function()
    local users = require("users")
    local s = spy.on(users, "info")
    users.info("chip0db4")
    assert.spy(users.info).was_called_with("chip0db4")
  end)
end)

我该如何模拟此过程,就像 Ruby 中的 Webmock 那样,在这里不会实际对接点?该解决方案不需要特别针对 busted 框架。

点赞
用户372599
用户372599

在收到TannerRogalsky的精彩反馈后,如https://gist.github.com/TannerRogalsky/b56bc886811f8f0a9d2a所示,我决定编写自己的http请求mocking库:https://github.com/chip/webmock。它仍处于早期阶段,但至少已经有了一个开始。我会感激有人为仓库做出贡献或提供其他方法或Lua模块的建议。

2014-06-29 02:04:51