lunarmodules / busted

Elegant Lua unit testing.
https://lunarmodules.github.io/busted/
MIT License
1.41k stars 186 forks source link

[question] how to pass argument to function when trying to catch errors #725

Closed mb6ockatf closed 1 year ago

mb6ockatf commented 1 year ago
describe("some asserts", function()
    it("should throw an error", function()
        assert.has_error(function throw_error() error("Yup,  it errored") end)
    end)
end)

here's a slightly modified example taken from official documentation. what if i want to pass arguments to function thow_error? how do i do that?

alerque commented 1 year ago

Wrap it in another function so that the function you pass to assert has all the function calls as you wish them to appear (passing any args to the actual functions that need testing):

local function throws_error(your, args, here)
    -- does something with args
    error("Yup,  it errored")
end
describe("some asserts", function()
    it("should throw an error", function()
        local function tester () throws_error(your, args, here) end
        assert.has_error(tester)
    end)
end)
mb6ockatf commented 1 year ago

thank you much! :smile: