lunarmodules / busted

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

spy `was_called_with` is not working as expected #634

Closed lstebner closed 4 years ago

lstebner commented 4 years ago

Hello, I am trying to spy on a method in a codebase where I am using lua's class style syntax, but using was_called_with is not ever working. When I change tests to was_called() they are fine, and when I inspect the spy I can see the expected argument in the calls, but yet was_called_with always says that the function was not called with expected arguments. I'm not finding any other people having this issue, so it could be just me, but I was able to set up a repo with a minimal amount of code that recreates my situation to help diagnose: https://github.com/lstebner/lua-busted-busted

a sidenote (which I did see brought up elsewhere) is that this failure message could be much more helpful by telling me what arguments it did get called with: "./thing_spec.lua:27: Function was not called with the arguments".

thank you in advance for any help!

Le0Developer commented 4 years ago

When calling thing:doStuff(whatStuff), you're actually calling doStuff like this thing.doStuff(thing, whatStuff).

I adjusted the check to assert.spy(thing.thing2.doStuff).was_called_with(thing.thing2, whatStuff) and it worked.

lstebner commented 4 years ago

@Le0Developer ahh, i should have thought of that! that actually makes a lot of sense, thank you very much for figuring that out. i won't get to try this out in my other project until much later tonight, but that sounds like it'll fix it for sure so i will close this issue.