Busted has_error导致测试错误

时间:2015-08-17 04:35:27

标签: unit-testing lua lua-busted

我试图在lua项目中使用被破坏的单元测试。我有一个看起来像这样的模块:

-- src/rom/apis/display.lua

local _displayModes = {
  single = 1,
  mirrored = 2,
  extended = 3,
  immersive = 4
}

local _validMode = function(mode)
  retVal = false
  for k,v in pairs(_displayModes) do
    if mode == v then retVal = true break end
  end
  return retVal
end

local _setMode = function (mode)
  if _validMode(mode) then
    _config.mode = mode
  else
    error("INVALID DISPLAY MODE: "..mode)
  end
end

display = {
  mode = _displayModes,
  setMode = _setMode
}

在我的规范中,我试图断言setMode返回错误:

local displayModule = require("rom/apis/display")
describe("#API #Display", function()
  describe("with single monitor", function()
    setup(function()
      local _p = {
        monitor = {'foo'}
      }
      mockPeripherals(_p)
    end)

    it("should not setMode with invalid mode", function()
      assert.has_error(display.setMode(100), "INVALID DISPLAY MODE: 100")
    end)
  end)
end)

当我运行规范时,由于函数中的错误而导致错误。这是控制台输出:

$ busted ●●●●✱ 4 successes / 0 failures / 1 error / 0 pending :
0.001444 seconds

Error → test/spec/rom/apis/display_spec.lua @ 32
#API #Display with single monitor should not setMode with invalid mode ./src/rom/apis/display.lua:32: INVALID DISPLAY MODE: 100

stack traceback:    ./src/rom/apis/display.lua:32: in function 'fn'     test/spec/rom/apis/display_spec.lua:34: in function <test/spec/rom/apis/display_spec.lua:32>

它出错我的错误,但问题是测试应该通过,因为我断言它正在返回正确的错误。

1 个答案:

答案 0 :(得分:3)

您需要在函数中包含错误调用:

sudo docker run -i -t ubuntu /bin/bash
Password:
Post http:///var/run/docker.sock/v1.20/containers/create: dial unix /var/run/docker.sock: no such file or directory.
* Are you trying to connect to a TLS-enabled daemon without TLS?
* Is your docker daemon up and running?
bash-4.3$