Skip to content

Instantly share code, notes, and snippets.

@JayMc
Created November 4, 2014 22:22
Show Gist options
  • Save JayMc/ca2a4aa0d330bc15b659 to your computer and use it in GitHub Desktop.
Save JayMc/ca2a4aa0d330bc15b659 to your computer and use it in GitHub Desktop.
mocha blogs test
/*var should = require('chai').should,
assert = require('chai').assert,*/
var should = require('should'),
assert = require('assert'),
request = require('supertest');
describe('Blogs', function(){
//our test blogs
var blog = {
title: 'blue'
}
var updatedBlog;
var invalidBlog = {
title: 'red'
}
it('should create test blog: '+blog.title, function(done){
request('http://localhost:3000')
.post('/blogs')
.send(blog)
.end(function(err, res){
if (err) {
throw err;
}
//console.log(res.body);
res.body.should.have.property('_id');
res.body.title.should.equal(blog.title);
res.body.date.should.not.equal(null);
//update out test blog with newly created _id
blog._id = res.body._id;
done();
});
});
// it('should return all blogs', function(done){
// this.timeout(60000);
// request('http://localhost:3000')
// .get('/blogs')
// .end(function(err, res){
// if(err)
// throw err;
// res.body.should.be.type('object');
// res.body.should.not.equal(null);
// done();
// });
// });
it('should update blog title', function(done){
//gets the _id from the test blog create
updatedBlog = blog;
updatedBlog.title = 'modified name';
request('http://localhost:3000')
.put('/blogs')
.send(updatedBlog)
.end(function(err, res){
res.body.should.have.property('_id');
res.body.title.should.equal(updatedBlog.title);
res.body.date.should.not.equal(null);
done();
});
});
it('should find test blog with updated title', function(done){
request('http://localhost:3000')
.get('/blogs/'+blog._id)
.end(function(err, res){
if(err)
throw err;
res.body.should.be.type('object');
res.body.title.should.equal(updatedBlog.title);
done();
});
});
it('should delete test blog', function(done){
request('http://localhost:3000')
.delete('/blogs/'+blog._id)
.end(function(err, res){
if(err)
throw err;
//console.log(res);
res.body.should.be.type('object');
res.body.should.empty;
done();
});
});
it('should not find test blog after it was deleted', function(done){
request('http://localhost:3000')
.get('/blogs/'+blog._id)
.end(function(err, res){
if(err)
throw err;
res.body.should.be.type('object');
res.body.should.be.empty;
done();
});
});
it('should receive a validation error', function(done){
request('http://localhost:3000')
.post('/blogs')
.send(invalidBlog)
.end(function(err, res){
if(err)
throw err;
//console.log(res.body);
res.body.should.have.property('errors')
res.body.name.should.equal('ValidationError')
done();
});
});
it('should not find the invalid blog in Database', function(done){
request('http://localhost:3000')
.post('/blogs-find/')
.send({keyword: invalidBlog.title})
//.send({keyword: 'blue'})
.end(function(err, res){
//console.log(res.body);
res.body.should.be.empty
done();
})
})
it('should get a random one', function(done){
request('http://localhost:3000')
.get('/blogs/random')
.end(function(err, res){
if(err)
throw err;
//console.log(res.body);
res.body.should.not.be.empty
done();
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment