lunarmodules / busted

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

Unable to get stub examples on site working #678

Closed ckipp01 closed 2 years ago

ckipp01 commented 2 years ago

Hello, I'm new to busted and am currently trying to understand a bit better how stubbing works as I'm trying to use it via plenary to test a Neovim plugin. I wasn't able to get it to work so I figured I'd just try with vanilla lua and busted, but I still had issues even with the example give on the website. For example on the webiste here.

describe("stubs", function()
  it("replaces an original function", function()
    local t = {
      greet = function(msg) print(msg) end
    }

    assert.stub(t, "greet")

    t.greet("Hey!") -- DOES NOT print 'Hey!'
    assert.stub(t.greet).was.called_with("Hey!")

    t.greet:revert()  -- reverts the stub
    t.greet("Hey!") -- DOES print 'Hey!'
  end)
end)

I did change stub to assert.stub

When trying this out locally with busted my_spec.lua I'm met with the following error:

❯ busted my_spec.lua
Hey!
✱
0 successes / 0 failures / 1 error / 0 pending : 0.001806 seconds

Error → my_spec.lua @ 2
stubs replaces an original function
/usr/local/share/lua/5.4/luassert/spy.lua:108: attempt to index a function value (local 'payload')

Looking at the code there I don't fully understand what's going on, and I'm hoping there is just something changed about the way you're supposed to stub that the docs aren't explaining. Is there something I'm missing? I'll happily update the docs if so.

System

Tieske commented 2 years ago

I did change stub to assert.stub

why? that's what causes it to break? The example as is works for me, your code above gives me the same errors.

ckipp01 commented 2 years ago

Total mishap on my end, thanks for the response!