Adding find functions and tests to comment model.

This commit is contained in:
David Jay
2016-11-03 17:06:15 -07:00
parent eefa8e3285
commit df77ee963a
6 changed files with 107 additions and 13 deletions
+3 -1
View File
@@ -1,10 +1,12 @@
/* eslint-env node, mocha */
'use strict';
// require('./utils/mongoose')
const expect = require('chai').expect;
describe('Comment', () => {
describe.only('#add', () => {
describe('#add', () => {
it('should add a comment', () => {
expect(0).to.be.equal(0);
});
+46
View File
@@ -0,0 +1,46 @@
/* eslint-env node, mocha */
require('../utils/mongoose');
const Comment = require('../../models/comment');
const expect = require('chai').expect;
describe('Comment: models', () => {
var mockComments
beforeEach(() => {
return Comment.create([{
body: 'comment 1',
asset_id: '123'
},{
body: 'comment 2',
asset_id: '123'
},{
body: 'comment 3',
asset_id: '456'
}]).then((comments) => {
mockComments = comments
});
});
describe('#findById()', () => {
it('should find a comment by id', () => {
return Comment.findById(mockComments[0].id).then((result) => {
expect(result).to.have.property('body')
.and.to.equal('comment 1');
});
});
});
describe('#findByAssetId()', () => {
it('should find an array of comments by asset id', () => {
return Comment.findByAssetId('123').then((result) => {
expect(result).to.have.length(2);
expect(result[0]).to.have.property('body')
.and.to.equal('comment 1');
expect(result[1]).to.have.property('body')
.and.to.equal('comment 2');
});
});
});
// });
});
+37
View File
@@ -0,0 +1,37 @@
// Modified from https://github.com/elliotf/mocha-mongoose
var mongoose = require('mongoose');
// ensure the NODE_ENV is set to 'test'
// this is helpful when you would like to change behavior when testing
process.env.NODE_ENV = 'test';
beforeEach(function (done) {
function clearDB() {
for (var i in mongoose.connection.collections) {
mongoose.connection.collections[i].remove(function() {});
}
return done();
}
if (mongoose.connection.readyState === 0) {
mongoose.connect('coral-talk-test', function (err) {
if (err) {
throw err;
}
return clearDB();
});
} else {
return clearDB();
}
});
after(function (done) {
mongoose.disconnect();
return done();
});