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

Automatic Sinon spies/stubs restoring #72

Closed
Reinmar opened this issue Oct 6, 2015 · 6 comments
Closed

Automatic Sinon spies/stubs restoring #72

Reinmar opened this issue Oct 6, 2015 · 6 comments
Labels

Comments

@Reinmar
Copy link
Member

Reinmar commented Oct 6, 2015

I want to propose that everytime we create a Sinon spy or stub that needs to be restored before the next test executes we use Sinon sandboxes like this:

var sandbox;

beforeEach( function() {
    sandbox = sinon.sandbox.create();
} );

afterEach( function() {
    sandbox.restore();
} );

it( 'whatever', function() {
    sandbox.stub( obj, 'foo' );
} );

I've been also considering extending benderjs-sinon with something like this (note, it must be testing framework agnostic):

afterEach( bender.restoreSpies() );

it( 'whatever', function() {
    bender.spies.push( sinon.stub( obj, 'foo' ) );
}

It'd be shorter, but it isn't that much simpler, so the default Sinon way looks satisfying.

Any thoughts?

@pjasiun
Copy link

pjasiun commented Oct 6, 2015

var sandbox;

beforeEach( function() {
    sandbox = sinon.sandbox.create();
} );

afterEach( function() {
    sandbox.restore();
} );

This looks like a repeated piece of code. So it should be defined one in the sinon or ckeditor bender plugin. DRY.

@Reinmar
Copy link
Member Author

Reinmar commented Oct 6, 2015

@pjasiun wrote in priv chat:

The problem is that sometimes we define spy for the whole test suit and sometimes for a single test

This is something I've been also aware, so I rejected things like https://www.npmjs.com/package/mocha-sinon which tries to automate the thing too much (I like to have a choice :D). The test author should be able to decide what should happen with a specific spy/stub. Both solutions that I proposed above allow you to make that decision.

On the other hand, one could argue that spies/stubs should never ever be shared between tests. This is also totally valid, so IMO we should avoid sharing them between tests. However, it's good to have a choice.

@Reinmar
Copy link
Member Author

Reinmar commented Oct 6, 2015

This looks like a repeated piece of code. So it should be defined one in the sinon or ckeditor bender plugin. DRY.

The problem here is that it cannot go to benderjs-sinon or benderjs-mocha, because it's a link between those two plugins. That's why in the solution I was going to propose you need to call afterEach() manually. So bender.restoreSpies() is exposed by the benderjs-sinon plugin and afterEach() of course by benderjs-mocha.

However, if we want to be super strict about DRY, then we can add something to CKE's plugin for Bender. But I'm not a big fun of this solution as you'll start repeating that in other projects using Bender and Sinon.

Therefore, I think that we need some simple mechanism and a convention. If you think that Sinon sandoboxes violate DRY, then perhaps the second option is fine?

@Reinmar
Copy link
Member Author

Reinmar commented Oct 6, 2015

I'm also checking if that could work (first tests are promising):

var sandbox = sinon.sandbox.create();

afterEach( sandbox.restore.bind( sandbox ) );

@Reinmar
Copy link
Member Author

Reinmar commented Oct 6, 2015

It seems to work fine. I'm OK with this approach.

@Reinmar
Copy link
Member Author

Reinmar commented May 5, 2017

We take care of this manually.

@Reinmar Reinmar closed this as completed May 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants