Closed crookse closed 2 years ago
I can confirm this is working now.
thanks for confirming @stav ! also, thank you for bringing this up!
Well I sure do love working on actively maintained software for just these kinds of contributions.
I actually did try and find the bug in the code but it was a bit over my head.
So I did this:
Rhum.testSuite("check", () => {
let calls
let xsocketMock
class XapiSocketMock {
check = check // code being tested
trades = () => { calls.trades++ }
}
Rhum.testCase("should call trades once", async () => {
calls = { trades: 0 }
xsocketMock = new XapiSocketMock()
await xsocketMock.check(data)
Rhum.asserts.assertEquals( calls.trades.length, 1 )
})
})
But now with your fix I can do:
Rhum.testSuite("check", () => {
let xSocketMock
class XapiSocketMock {
check = check
trades = () => {}
}
Rhum.testCase("should call trades once", async () => {
xSocketMock = Rhum.mock(XapiSocketMock).create()
await xSocketMock.check(data)
Rhum.asserts.assertEquals( xSocketMock.calls.trades, 1 )
})
})
Summary
See comment from Discord:
Root Cause
The issue is with this block of code in
mock_builder.ts#create()
:The above code turns every method in an object into a method that can track itself. For example:
becomes
In the problematic block, there is this:
This makes each method being mocked call the ORIGINAL object's method. So, what's happening in this issue is:
<real object>.add()
is replaced with the wrapper function that can track calls viamock.calls[method]++
mock.add()
is calledmock.add()
callsmock.calls["add"]++
andmock.calls.add === 1
nowmock.add()
calls the original version of itselfmock.add()
calls the original version ofrealAdd()
realAdd()
doesn't havemock.calls["realAdd"]++
, somock.calls.realAdd === 0
Example test code