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

Sandbox object not accessible on context #57

Closed
peterjwest opened this issue May 3, 2017 · 6 comments
Closed

Sandbox object not accessible on context #57

peterjwest opened this issue May 3, 2017 · 6 comments

Comments

@peterjwest
Copy link

It would be nice to be able to access the sandbox object from the context object - particularly it would be useful to call .reset() on the sandbox for repeated tests.

@shaunc
Copy link
Contributor

shaunc commented May 25, 2017

Hmm... Look at lib/default-config.js. properties is a list of properties of the sandbox to inject into the context. I think all you need to do is add copy this properties list and call configureTest with the list of default properties expanded with "reset".

Tell me if it works. (They roped me into being a contributor after I submitted a PR for something --- but I'm still still not quite at home with the coding style of the internals.... :))

A minimal enhancement might be to add (and document) an "extraSandboxProperties" option that would list any additional properties you wanted to add, without having to copy the list of default properties.

@peterjwest
Copy link
Author

I'll have a go, cheers!

@shaunc
Copy link
Contributor

shaunc commented May 25, 2017

[The alternative enhancement would be just to add "reset" to the list of default properties. @fatso83 ?]

@fatso83
Copy link
Contributor

fatso83 commented May 26, 2017

I played around with this a bit and found that adding 'reset' is not sufficient, but I also found that you can expose the sandbox object by adding 'sandbox' to the list of properties. Unfortunately, this seems to have exposed a bug, where calling sandbox.reset will crash in fake_server.js where the responses array has not been initialized.

@fatso83
Copy link
Contributor

fatso83 commented May 26, 2017

Fixed the bug causing a crash in sinonjs/sinon#1428.

Back to the actual issue here, specifying properties: ['sandbox', 'stub'] on the configure call will expose the sandbox and its reset method:

var sinon = require('sinon');
var sinonTest = require('sinon-test');
sinon.test = sinonTest.configureTest(sinon, { 
    properties: ["stub", 'sandbox'] ,
    useFakeServer: false  // workaround for sinon#1428
});
var obj = { 'foo' : () => 'foo-val' }
console.log('before stub', obj.foo());

var testSuite = {}

testSuite.myTest = sinon.test( function() {
    var stub = this.stub(obj, 'foo').callsFake( () => 'fake-val' );
    console.log(obj.foo());
    console.log('callCount:',stub.callCount, stub.called); // => 1
    this.sandbox.reset();
    console.log('callCount:',stub.callCount); // => 0
});
testSuite.myTest();

@peterjwest
Copy link
Author

Brilliant, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants