Reputation: 3295
I've started using Sinon.js to mock a MongoDB library in a Mocha test suite. I'm confused as to why mock.restore()
in my afterEach
blocks do not actually clear out the mocks & assertions I've set up in other tests. Example:
mockedMongo.expects('updateCustomer').once();
mockedMongo.restore();
mockedMongo.expects('updateCustomer').never();
mockedMongo.verify(); // error here
The last line will throw an Expected updateCustomer([...]) once (never called)
ExpectationError. In the documentation it says that mock.restore()
"Restores all mocked methods". I'm trying to figure out what that actually means, since it doesn't clear out my previous expectation, even when it seems that I've overwritten the mock on that method with something else. Thoughts?
Upvotes: 1
Views: 411
Reputation: 45840
Summary
If any methods have been wrapped in a proxy by the mock, restore()
returns them to their original state. That's all it does.
Details
Looking at the source gives the following info:
expects()
sets up a proxy
for the method if no expectations
have been set on it yet, and then adds an expectation
verify()
cycles the expectations on the proxies and verifies each, then calls restore()
restore()
cycles the proxies and restores the original methodsAll restore()
does is remove any proxies added by expects()
, it doesn't affect the expectations
stored by the mock.
So for each line in your example code:
updateCustomer
and add expectation
of once
updateCustomer
expectation
of never
to updateCustomer
expectations
on updateCustomer
and record that once
fails, call restore()
, then report that once
failedUpvotes: 2