jupiter / simple-mock

Super simple stubs and spies with 1-step sandbox restore
MIT License
87 stars 12 forks source link

Method to reset a spy #1

Closed solsson closed 9 years ago

solsson commented 9 years ago

I felt the need to reset a specific spy to its initial state, with no calls logged.

Right now doing:

var simple = require('simple-mock');
// reset a single functino spy in simple-mock, the brutal one-liner way, works for this spec's cases
var reset = function(simpleSpy) {
  simpleSpy.calls.length = simpleSpy.callCount = simpleSpy.called = simpleSpy.lastCall = 0;
};

Is there such a method already, that doesn't affect other mocks? If not I can suggest one in my fork.

jupiter commented 9 years ago

Thanks, I've made some changes. Let me know if this works for you.

solsson commented 9 years ago

Works as expected.

For now the pattern how I use the lib looks like:

var mocks = require('simple-mock');

var someTestObject = {
  ...
  someFunction: mocks.spy(function() { return 'a-return-value-if-desired'; })
  ...
};

// somewhere is a procedure-like Mocha test
  expect(someTestObject.someFunction.called).to.be.true();
  someTestObject.someFunction.reset(); // to simplify for it:s below

I'm guessing sequences of returnWith will come in handy soon.

Thanks for writing such a nicely scoped lib.

jupiter commented 9 years ago

Glad that's what you need. Thanks for the feedback.

In your example, changing to mocks.stub().returnWith('a-return-value-if-desired') wouldn't be a big improvement, but if you were expecting multiple calls with different return values, it could help.