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

Support for sinon sandboxes #15

Closed
jniemin opened this issue Apr 7, 2017 · 9 comments
Closed

Support for sinon sandboxes #15

jniemin opened this issue Apr 7, 2017 · 9 comments

Comments

@jniemin
Copy link
Contributor

jniemin commented Apr 7, 2017

Seems that if creating mock through sandbox, the chainable is not assigned.

var sandbox = sinon.sandbox.create();
var Mock = sandbox.mock(MyMongooseObject);
Mock.expect('findOne').chain('populate');

This example fails with error:
TypeError: Mock.expects(...).chain is not a function

Using sandbox is more convenient as you can easily just create as many mocks you like and then do sandbox.restore which will restore all mocks rather than restoring each mock separately.

@gaguirre
Copy link
Collaborator

gaguirre commented Apr 7, 2017

@jniemin it should work... actually I always use sandbox.
Could you post a complete example?

@jniemin
Copy link
Contributor Author

jniemin commented Apr 10, 2017

Here is complete the example @gaguirre. I'm using Mocha 3.2.0, sinon 2.1.0, and sinon-mongoose 1.3.0

roles.js

"use strict";

const mongoose = require('mongoose');
const Schema   = mongoose.Schema;

const ROLES_SCHEMA = new Schema({
  name:{
    type:String
  }
},{
  timestamps: {
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  }
});

const Role = mongoose.model('Role', ROLES_SCHEMA);

module.exports = Role;

roles.spec.js

"use strict";

const Role = require('./roles');
const sinon = require('sinon');
require('sinon-mongoose');
const sinonSandbox = sinon.sandbox;

describe("sinon-mongoose sandbox", function(){
  afterEach(function() {
    sinonSandbox.restore();
  });

  it("Chaining should work when using sinon sandbox", function(){
    // throws error "TypeError: RoleMock.expects(...).chain is not a function"
    const RoleMock = sinonSandbox.mock(Role);
    RoleMock.expects('findOne').chain('populate').resolves({});
  });

  it("Chaining works when using mock not created by sandbox", function(){
    const RoleMock = sinon.mock(Role);
    RoleMock.expects('findOne').chain('populate').resolves({});
    RoleMock.restore();
  })

});

@gaguirre
Copy link
Collaborator

You're missing the sandbox creation. It should be

const sinonSandbox = sinon.sandbox.create()

Tell me if it works!

@jniemin
Copy link
Contributor Author

jniemin commented Apr 10, 2017

Ah, copy paste error, when I removed some of the business names etc. So sandbox.create() is in the actual test code. So still no luck

@gaguirre
Copy link
Collaborator

Weird. There is a test using sandbox (https://github.com/underscopeio/sinon-mongoose/blob/master/test/index.js#L116).
Check it out, it may be helpful.

@jniemin
Copy link
Contributor Author

jniemin commented Apr 11, 2017

Seems that if I ran sinon-mongoose tests from master there it works. I wonder could it be sinon-mongoose using older versions of sinon (1.17.7) and mocha (2.5.3)? I'm running on my own project sinon 2.1.0 and mocha 3.2.0.

@gaguirre
Copy link
Collaborator

Maybe... Could you try upgrading sinon for the tests and see if it fails.
Then probably it needs a fix for newer sinon versions.
Thanks @jniemin!

@jniemin
Copy link
Contributor Author

jniemin commented Apr 12, 2017

It definitely was then new version of Sinon that made the tests break. I created PR to add support for Sinon 2.

@jniemin
Copy link
Contributor Author

jniemin commented Apr 28, 2017

Actual implementation was not totally proper. There is now new PR to fix that

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

No branches or pull requests

2 participants