Skip to content

Commit

Permalink
Enhacements to article controller tests by Ahmed
Browse files Browse the repository at this point in the history
  • Loading branch information
chaudhryjunaid committed Dec 13, 2015
1 parent 5ad804b commit a59bedd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 52 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
},
"dependencies": {
"async": "latest",
"bluebird": "^3.0.6",
"body-parser": "^1.14.1",
"bower": "latest",
"compression": "^1.6.0",
Expand Down
95 changes: 44 additions & 51 deletions test/mocha/controllers/articleControllerSpec.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
/**
* Created by Ahmed Hassan on 12/11/15.
* Set of tests demonstrating how controllers can be tested by stubbing out database dependency
* The tests are pretty basic, intended for demonstration of technique
*/
"use strict";

var proxyquire = require('proxyquire'),
var proxyquire = require('proxyquire').noCallThru(),
dbStub = {},

article = proxyquire('../../../app/controllers/articles', {
'../../config/sequelize': dbStub
}),
chai = require('chai'),
Promise = require('bluebird');
Promise = require('sequelize').Promise;

chai.should();
var expect = chai.expect;

var req = {};
var res = {};

describe('Articles Controller', function () {

describe('Checking functionality of Articles Controller', function () {

describe('Testing Create Article', function () {
describe('Create Article', function () {

beforeEach(function () {

Expand All @@ -34,7 +35,7 @@ describe('Checking functionality of Articles Controller', function () {

});

it('should successfully create an article ', function (done) {
it('should return article on successful creation', function (done) {

dbStub.Article = {
create: function (obj) {
Expand Down Expand Up @@ -76,7 +77,7 @@ describe('Checking functionality of Articles Controller', function () {

});

it('should return error if error occur while executing query', function (done) {
it('should return error if error occurs while executing query', function (done) {

dbStub.Article = {
create: function (obj) {
Expand All @@ -101,7 +102,7 @@ describe('Checking functionality of Articles Controller', function () {

});

describe('Testing Update Article', function () {
describe('Update Article', function () {

beforeEach(function () {
req = {
Expand Down Expand Up @@ -136,14 +137,11 @@ describe('Checking functionality of Articles Controller', function () {

});

it('should successfully create an article ', function (done) {
it('should return updated article on success', function (done) {


res.jsonp = function (updatedArticle) {

//
updatedArticle.should.have.property('id');

updatedArticle.should.have.property('title');
updatedArticle.title.should.equal('test title updated');
updatedArticle.should.have.property('content');
Expand All @@ -156,10 +154,11 @@ describe('Checking functionality of Articles Controller', function () {

});

it('should return error if error occur while executing query', function (done) {
it('should return error if error occurs while executing query', function (done) {
req.article.updateAttributes = function () {

return Promise.reject('error occur while executing query');
// note: the rejection value here is symbolic,
// the key thing is that the same error should be propagated ahead
return Promise.reject('error occurred while executing query');
};


Expand All @@ -169,7 +168,7 @@ describe('Checking functionality of Articles Controller', function () {
err.should.equal('error');
obj.should.have.property('error');
obj.should.have.property('status');
obj.error.should.equal('error occur while executing query');
obj.error.should.equal('error occurred while executing query');
obj.status.should.equal(500);

done();
Expand All @@ -182,7 +181,7 @@ describe('Checking functionality of Articles Controller', function () {
});


describe('Testing Destroy Article', function () {
describe('Destroy Article', function () {

beforeEach(function () {
req = {
Expand All @@ -192,15 +191,13 @@ describe('Checking functionality of Articles Controller', function () {
content: 'this is test content to test functionality of destroy Article',
UserId: 1,
destroy: function () {

return Promise.resolve();
}
}
}
});

it('should successfully destroy an article ', function (done) {

it('should return destroyed article on successful destroy', function (done) {

res.jsonp = function (article) {

Expand All @@ -217,20 +214,16 @@ describe('Checking functionality of Articles Controller', function () {

});

it('should return error if error occur while executing query', function (done) {
it('should return error if error occurs while executing query', function (done) {
req.article.destroy = function () {

return Promise.reject('error occur while executing query');
return Promise.reject('error occurred while executing query');
};


res.render = function (err, obj) {


err.should.equal('error');
obj.should.have.property('error');
obj.should.have.property('status');
obj.error.should.equal('error occur while executing query');
obj.error.should.equal('error occurred while executing query');
obj.status.should.equal(500);

done();
Expand All @@ -243,60 +236,60 @@ describe('Checking functionality of Articles Controller', function () {
});


describe('Testing fetch all articles', function () {
describe('Fetch all articles', function () {


it('should successfully destroy an article ', function (done) {
it('should return the received list of articles on successful query', function (done) {

dbStub.Article = {
findAll: function (obj) {
var Article = {
var Articles = [{
id: 1,
title: 'test',
content: 'this is test content',
UserId: 1
};
},{
id: 2,
title: 'test',
content: 'this is test content',
UserId: 1
}];

return Promise.resolve(Article);
return Promise.resolve(Articles);
}
};
dbStub.User = {id: 1};

res.jsonp = function (article) {

article.should.have.property('id');
article.should.have.property('title');
article.title.should.equal('test');
article.should.have.property('content');
article.content.should.equal('this is test content');
article.should.have.property('UserId');
res.jsonp = function (articles) {
articles[0].should.have.property('id');
articles[0].should.have.property('title');
articles[0].title.should.equal('test');
articles[0].should.have.property('content');
articles[0].content.should.equal('this is test content');
articles[0].should.have.property('UserId');
done();
};

article.all(req, res);

});
it('should return error if error occur while executing query', function (done) {
it('should return error if error occurs while executing query', function (done) {

dbStub.Article = {
findAll: function (obj) {

return Promise.reject('error occur while executing query');
return Promise.reject('error occurred while executing query');
}
};
dbStub.User = {id: 1};

res.render = function (err, obj) {


err.should.equal('error');
obj.should.have.property('error');
obj.should.have.property('status');
obj.error.should.equal('error occur while executing query');
obj.error.should.equal('error occurred while executing query');
obj.status.should.equal(500);

done();

};

article.all(req, res);
Expand All @@ -305,10 +298,10 @@ describe('Checking functionality of Articles Controller', function () {

});

describe('Testing Has Authorization', function () {
describe('Has Authorization', function () {


it('should gie error if user not authorized', function (done) {
it('should give error if user is not authorized', function (done) {

req = {
article: {
Expand All @@ -321,10 +314,10 @@ describe('Checking functionality of Articles Controller', function () {
}

},
res.send = function (err, msg) {
res.send = function (httpStatus, msg) {


err.should.equal(401);
httpStatus.should.equal(401);
msg.should.equal('User is not authorized');

done();
Expand Down

0 comments on commit a59bedd

Please sign in to comment.