From 22eabf6620350a36e241c8bbc468d9c31bd2a2f4 Mon Sep 17 00:00:00 2001 From: Denis Wolf Date: Sun, 17 Nov 2013 23:35:18 +0200 Subject: [PATCH 1/4] tests: topic.js: naming flow fix for .post --- tests/topics.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/tests/topics.js b/tests/topics.js index 3b7655fbbf..149063bd0f 100644 --- a/tests/topics.js +++ b/tests/topics.js @@ -1,6 +1,3 @@ - - - var winston = require('winston'); process.on('uncaughtException', function (err) { @@ -19,19 +16,19 @@ reds.createClient = function () { var Topics = require('../src/topics'); -describe('Topics', function() { +describe('Topic\'s', function() { var newTopic; var newPost; var userInfo; describe('.post', function() { - it('should post a new topic', function(done) { - var uid = 1, - cid = 1, + it('should create a new topic with proper parameters', function(done) { + var userId = 1, + categoryId = 1, title = 'Test Topic Title', content = 'The content of test topic'; - Topics.post(uid, title, content, cid, function(err, result) { + Topics.post(userId, title, content, categoryId, function(err, result) { assert.equal(err, null, 'was created with error'); assert.ok(result); @@ -42,7 +39,7 @@ describe('Topics', function() { }); }); - it('should fail posting a topic', function(done) { + it('should fail to create new topic with wrong parameters', function(done) { var uid = null, cid = 1, title = 'Test Topic Title', From 6893bd8b046ef9da33f9efe90a5e2dde10a8d242 Mon Sep 17 00:00:00 2001 From: Denis Wolf Date: Sun, 17 Nov 2013 23:41:38 +0200 Subject: [PATCH 2/4] tests: topic.js: extract mock data init in .post --- tests/topics.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/tests/topics.js b/tests/topics.js index 149063bd0f..de875ab868 100644 --- a/tests/topics.js +++ b/tests/topics.js @@ -19,16 +19,21 @@ var Topics = require('../src/topics'); describe('Topic\'s', function() { var newTopic; var newPost; - var userInfo; describe('.post', function() { - it('should create a new topic with proper parameters', function(done) { - var userId = 1, - categoryId = 1, - title = 'Test Topic Title', - content = 'The content of test topic'; + var topic; + + beforeEach(function(){ + topic = { + userId: 1, + categoryId: 1, + title: 'Test Topic Title', + content: 'The content of test topic' + }; + }); - Topics.post(userId, title, content, categoryId, function(err, result) { + it('should create a new topic with proper parameters', function(done) { + Topics.post(topic.userId, topic.title, topic.content, topic.categoryId, function(err, result) { assert.equal(err, null, 'was created with error'); assert.ok(result); @@ -40,12 +45,9 @@ describe('Topic\'s', function() { }); it('should fail to create new topic with wrong parameters', function(done) { - var uid = null, - cid = 1, - title = 'Test Topic Title', - content = 'The content of test topic'; + topic.userId = null; - Topics.post(uid, title, content, cid, function(err, result) { + Topics.post(topic.userId, topic.title, topic.content, topic.categoryId, function(err, result) { assert.equal(err.message, 'not-logged-in'); done(); }); From cbbb7a7c8eebc892b241a20ec283909c96069ffa Mon Sep 17 00:00:00 2001 From: Denis Wolf Date: Sun, 17 Nov 2013 23:50:19 +0200 Subject: [PATCH 3/4] tests: topic.js: extract mock data init in getters --- tests/topics.js | 57 +++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/tests/topics.js b/tests/topics.js index de875ab868..16bc92ee32 100644 --- a/tests/topics.js +++ b/tests/topics.js @@ -17,29 +17,24 @@ reds.createClient = function () { var Topics = require('../src/topics'); describe('Topic\'s', function() { - var newTopic; - var newPost; + var topic; + + beforeEach(function(){ + topic = { + userId: 1, + categoryId: 1, + title: 'Test Topic Title', + content: 'The content of test topic' + }; + }); describe('.post', function() { - var topic; - - beforeEach(function(){ - topic = { - userId: 1, - categoryId: 1, - title: 'Test Topic Title', - content: 'The content of test topic' - }; - }); it('should create a new topic with proper parameters', function(done) { Topics.post(topic.userId, topic.title, topic.content, topic.categoryId, function(err, result) { assert.equal(err, null, 'was created with error'); assert.ok(result); - newTopic = result.topicData; - newPost = result.postData; - done(); }); }); @@ -54,25 +49,35 @@ describe('Topic\'s', function() { }); }); - describe('.getTopicData', function() { - it('should get Topic data', function(done) { - Topics.getTopicData(newTopic.tid, function(err, result) { - done.apply(this.arguments); + describe('Get methods', function() { + var newTopic; + var newPost; + + beforeEach(function(done){ + Topics.post(topic.userId, topic.title, topic.content, topic.categoryId, function(err, result) { + newTopic = result.topicData; + newPost = result.postData; + done(); }); }); - }); - - describe('.getTopicDataWithUser', function() { - it('should get Topic data with user info', function(done) { - Topics.getTopicDataWithUser(newTopic.tid, function(err, result) { + describe('.getTopicData', function() { + it('should get Topic data', function(done) { + Topics.getTopicData(newTopic.tid, function(err, result) { + done.apply(this.arguments); + }); + }); + }); - done.apply(this.arguments); + describe('.getTopicDataWithUser', function() { + it('should get Topic data with user info', function(done) { + Topics.getTopicDataWithUser(newTopic.tid, function(err, result) { + done.apply(this.arguments); + }); }); }); }); - after(function() { RDB.send_command('flushdb', [], function(error){ if(error){ From 7c3fa30c1304efb9244895de02d72b529a790c21 Mon Sep 17 00:00:00 2001 From: Denis Wolf Date: Mon, 18 Nov 2013 00:00:22 +0200 Subject: [PATCH 4/4] tests: topic.js: fixed description to mirror code, refactored asserts since mocha's 'done' can process errors in callback --- tests/topics.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tests/topics.js b/tests/topics.js index 16bc92ee32..063451cc3e 100644 --- a/tests/topics.js +++ b/tests/topics.js @@ -62,18 +62,14 @@ describe('Topic\'s', function() { }); describe('.getTopicData', function() { - it('should get Topic data', function(done) { - Topics.getTopicData(newTopic.tid, function(err, result) { - done.apply(this.arguments); - }); + it('should not receive errors', function(done) { + Topics.getTopicData(newTopic.tid, done); }); }); describe('.getTopicDataWithUser', function() { - it('should get Topic data with user info', function(done) { - Topics.getTopicDataWithUser(newTopic.tid, function(err, result) { - done.apply(this.arguments); - }); + it('should not receive errors', function(done) { + Topics.getTopicDataWithUser(newTopic.tid, done); }); }); });