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

Feature request: sandbox.createStubInstance() #1598

Closed
dpmott opened this issue Oct 26, 2017 · 2 comments
Closed

Feature request: sandbox.createStubInstance() #1598

dpmott opened this issue Oct 26, 2017 · 2 comments

Comments

@dpmott
Copy link
Contributor

dpmott commented Oct 26, 2017

  • Sinon version : 4.0.1
  • Environment : Windows 10
  • Example URL :
  • Other libraries you are using: Karma, Jasmine, Typescript

What did you expect to happen?
I was hoping to find an easy way to reset all of the stubs in an object returned by createStubInstance().
Given that the sandbox mirrors most methods that of stubs and spies, I was hoping to find a method like sandbox.createStubInstance() that would create an object whose stubbed methods were tied to the sandbox.

What actually happens
sinon.createStubInstance() creates independent stubs for each method, which appear to not be associated with any sandbox to which I have access. There is no analogous sandbox method.

How to reproduce

I've made a utility helper function in my own code to achieve this:

export const createStubInstance = (constructor: any, sandbox: any): any => {
  const stub = sinon.createStubInstance(constructor);
  for (const property in stub) {
    if (stub.hasOwnProperty(property)) {
      stub[property] = sandbox.stub();
    }
  }

  return stub;
};

Then, whenever I call sandbox.reset(), all of the stubs in the object created by the call to createStubInstance() will be reset (for behavior and history).

This lets me write cleaner code:

  const sandbox = sinon.sandbox.create();
  const stubNgbModal = createStubInstance(NgbModal, sandbox);

  beforeEach(() => {
      stubNgbModal.open.returns({ result: Promise.resolve() });
  });

  afterEach(() => {
    sandbox.reset();
  });

whereas before, this code would either have assigned a new sandbox.stub() before every test:

   stubNgbModal.open = sandbox.stub().returns({ result: Promise.resolve() }); // another allocated stub

or the afterEach would need to clean up the stub by-name:

   stubNgbModal.open.reset(); // a line like this for every customized stub

What now?
I'm rather a novice with using Sinon; if there's a better way to achieve what I'm trying to do, I'd love to hear about it.

If there's interest in this feature request, I'll spend some time to create a pull request.

Thanks for reading!

@mroderick
Copy link
Member

I think that having createStubInstance on sandbox would be a natural improvement. A pull request that implements this would be most welcome!

In your helper you're using for..in and hasOwnProperty. Sinon uses ES5.1 which includes Object.keys and Array.prototype.forEach, which would be preferred over for...in and hasOwnProperty.

Your PR should include an update to the documentation in docs/release-source/release/sandbox.md also.

Looking forward to your PR 🤸‍♂️

@mroderick
Copy link
Member

This was implemented with #1602 and published to npm as sinon@4.1.0

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

2 participants