Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert.stub/spy do not accept a function argument #3

Open
mliszcz opened this issue May 21, 2023 · 1 comment
Open

assert.stub/spy do not accept a function argument #3

mliszcz opened this issue May 21, 2023 · 1 comment

Comments

@mliszcz
Copy link

mliszcz commented May 21, 2023

This is an example taken directly from busted documentation:

    local t = {
      greet = function(msg) print(msg) end
    }

    stub(t, "greet")

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

Lua LS gives the following error on the assertion line:

Diagnostics:                                                        
Cannot assign `function` to parameter `luassert.spy`.               
- `function` cannot match `luassert.spy`                            
- Type `function` cannot match `luassert.spy` [param-type-mismatch]

I'm not sure how to fix this. Changing the param in the meta file to luassert.spy|function may be a workaround but would not be accurate. The problem is that t.greet is already a luassert.spy object but the language server incorrectly assumes that it is a function. The object is modified in-place and after the call:

local s = stub(t, "greet")

t.greet is no longer a function but a callable table. Both s and t.greet point to the same object (of type luassert.spy). It would be best if the language server could figure this out.

@AndreasMatthias
Copy link

Here's a workaround:

 assert.stub(t.greet --[[@as luassert.spy]]).was.called_with("Hey!")

Unfortunately, you have to do this for each assert.stub() call. This is awkward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants