Skip to content

Commit

Permalink
Merge from master
Browse files Browse the repository at this point in the history
  • Loading branch information
jniemin committed Apr 28, 2017
2 parents acc9641 + 37fd5ab commit 7522520
Show file tree
Hide file tree
Showing 5 changed files with 3,321 additions and 112 deletions.
8 changes: 1 addition & 7 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@
2,
"always"
],
"space-before-function-paren": [
2,
{
"anonymous": "always",
"named": "never"
}
],
"space-in-parens": [
2,
"never"
Expand Down Expand Up @@ -110,6 +103,7 @@
"no-shadow-restricted-names": 2,
"no-undef-init": 2,
"camelcase": 2,
"comma-dangle": 0,
"comma-spacing": 2,
"new-cap": 2,
"new-parens": 2,
Expand Down
3 changes: 1 addition & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function chainMethod(type) {
function makeChainable(mock, mockType) {
var expectsMethod = mock.expects;

mock.expects = function (method) {
mock.expects = function(method) {
mockType = mockType || (method === 'aggregate' ? 'aggregate' : 'query');
var expectation = expectsMethod.apply(mock, arguments);
expectation.chain = chainMethod(mockType).bind(expectation);
Expand Down Expand Up @@ -45,4 +45,3 @@ sinon.sandbox.mock = function(object){

return this.add(mockResult);
};

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sinon-mongoose",
"version": "1.3.0",
"version": "2.0.0",
"description": "Sinon extensions for Mongoose stubs",
"homepage": "",
"author": {
Expand Down
162 changes: 60 additions & 102 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,135 +4,98 @@ var mongoose = require('mongoose');

require('../lib');

describe('sinon-mongoose', function () {

describe('sinon-mongoose', function() {
var bookSchema = new mongoose.Schema({
title: String
title: String,
});

var Book = mongoose.model('Book', bookSchema);

describe('should made Mongoose model methods chainables', function () {

it('#find', function (done) {
describe('should made Mongoose model methods chainables', function() {
it('#find', function(done) {
var BookMock = sinon.mock(Book);

BookMock
.expects('find').withArgs('SOME_ARGUMENTS')
.chain('exec')
.resolves('RESULT');

Book.find('SOME_ARGUMENTS')
.exec()
.then(function (result) {
BookMock.verify();
BookMock.restore();
assert.equal(result, 'RESULT');
done();
});
BookMock.expects('find').withArgs('SOME_ARGUMENTS').chain('exec').resolves('RESULT');

Book.find('SOME_ARGUMENTS').exec().then(function(result) {
BookMock.verify();
BookMock.restore();
assert.equal(result, 'RESULT');
done();
});
});

it('#lean', function (done) {
it('#lean', function(done) {
var BookMock = sinon.mock(Book);

BookMock
.expects('find').withArgs('SOME_ARGUMENTS')
.chain('lean')
.chain('exec')
.resolves('RESULT');

Book.find('SOME_ARGUMENTS')
.lean()
.exec()
.then(function (result) {
BookMock.verify();
BookMock.restore();
assert.equal(result, 'RESULT');
done();
});
BookMock.expects('find').withArgs('SOME_ARGUMENTS').chain('lean').chain('exec').resolves('RESULT');

Book.find('SOME_ARGUMENTS').lean().exec().then(function(result) {
BookMock.verify();
BookMock.restore();
assert.equal(result, 'RESULT');
done();
});
});

it('#count', function (done) {
it('#count', function(done) {
var BookMock = sinon.mock(Book);

BookMock
.expects('find').withArgs('SOME_ARGUMENTS')
.chain('count')
.chain('exec')
.resolves('RESULT');

Book.find('SOME_ARGUMENTS')
.count()
.exec()
.then(function (result) {
BookMock.verify();
BookMock.restore();
assert.equal(result, 'RESULT');
done();
});
BookMock.expects('find').withArgs('SOME_ARGUMENTS').chain('count').chain('exec').resolves('RESULT');

Book.find('SOME_ARGUMENTS').count().exec().then(function(result) {
BookMock.verify();
BookMock.restore();
assert.equal(result, 'RESULT');
done();
});
});

it('#aggregate', function (done) {
it('#aggregate', function(done) {
var BookMock = sinon.mock(Book);

BookMock
.expects('aggregate')
.chain('lookup')
.resolves('RESULT');

Book.aggregate()
.lookup()
.then(function (result) {
BookMock.verify();
BookMock.restore();
assert.equal(result, 'RESULT');
done();
});
BookMock.expects('aggregate').chain('lookup').resolves('RESULT');

Book.aggregate().lookup().then(function(result) {
BookMock.verify();
BookMock.restore();
assert.equal(result, 'RESULT');
done();
});
});
});

describe('should made Mongoose document methods chainables', function () {

it('#update', function (done) {
describe('should made Mongoose document methods chainables', function() {
it('#update', function(done) {
var bookMock = sinon.mock(new Book({ title: 'Rayuela' }));

bookMock
.expects('update').withArgs('SOME_ARGUMENTS')
.chain('exec')
.resolves('RESULT');

bookMock.object.update('SOME_ARGUMENTS')
.exec()
.then(function (result) {
bookMock.verify();
bookMock.restore();
assert.equal(result, 'RESULT');
done();
});
bookMock.expects('update').withArgs('SOME_ARGUMENTS').chain('exec').resolves('RESULT');

bookMock.object.update('SOME_ARGUMENTS').exec().then(function(result) {
bookMock.verify();
bookMock.restore();
assert.equal(result, 'RESULT');
done();
});
});
});

describe('using sinon sandbox', function () {
describe('using sinon sandbox', function() {
var sandbox = sinon.sandbox.create();

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

it('should work mocking Model', function () {
it('should work mocking Model', function() {
var BookMock = sandbox.mock(Book);

BookMock
.expects('find').withArgs('SOME_ARGUMENTS')
.chain('exec')
.resolves('RESULT');
BookMock.expects('find').withArgs('SOME_ARGUMENTS').chain('exec').resolves('RESULT');

Book.find('SOME_ARGUMENTS')
.exec()
.then(function (result) {
assert.equal(result, 'RESULT');
});
Book.find('SOME_ARGUMENTS').exec().then(function(result) {
assert.equal(result, 'RESULT');
});
});

it('Model should be restored properly', function () {
Expand All @@ -146,16 +109,11 @@ describe('sinon-mongoose', function () {
it('should work mocking Document', function () {
var bookMock = sandbox.mock(new Book({ title: 'Rayuela' }));

bookMock
.expects('update').withArgs('SOME_ARGUMENTS')
.chain('exec')
.resolves('RESULT');
bookMock.expects('update').withArgs('SOME_ARGUMENTS').chain('exec').resolves('RESULT');

bookMock.object.update('SOME_ARGUMENTS')
.exec()
.then(function (result) {
assert.equal(result, 'RESULT');
});
bookMock.object.update('SOME_ARGUMENTS').exec().then(function(result) {
assert.equal(result, 'RESULT');
});
});
});
});
Loading

0 comments on commit 7522520

Please sign in to comment.