在Lua中使用busted时,模块无法访问。

我是一个 Lua 的初学者,我想在我的开发中使用单元测试。我决定使用 busted 这个简单易用的框架来实现。

require "yaci"
require "busted"

foo = {}
foor.bar = newclass( "foo.bar" )
function foo.bar:doSomething() return "foo bar" end

describe("Unit tests for Foo.Bar", function()

    it("A first test", function()

        local to_test = foo.bar()
        local text = to_test:doSomething()

        local a = { test = say }
        local b = { test = "foo bar" }
        assert.same( a, b )

    end)

end

但是 foo.bar 似乎无法访问...

attempt to index global 'foo' (a nil value)

而在 describe 之外没有任何问题。

有人能解释一下为什么在 describe 中 foo.bar 无法访问吗?

谢谢。

点赞
用户298661
用户298661

最有可能的是,describe 函数会设置传递函数的环境,以防它干扰其他代码文件。

2012-09-18 12:37:09