From 3e8473027739a788ed3401d67f875fb2529174c8 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 16 Apr 2015 00:08:53 +0900 Subject: [PATCH] Change assert.fail() to using sinon.spy() --- test/scripts/box/box.js | 29 ++++++++---- test/scripts/extend/filter.js | 17 +++++-- test/scripts/extend/tag.js | 20 ++++++-- test/scripts/filters/new_post_path.js | 10 ++-- test/scripts/helpers/partial.js | 11 +++-- test/scripts/hexo/hexo.js | 9 ++-- test/scripts/hexo/locals.js | 38 +++++++++++---- test/scripts/hexo/render.js | 17 +++++-- test/scripts/hexo/router.js | 63 ++++++++++++++++++------- test/scripts/models/asset.js | 20 ++++---- test/scripts/models/cache.js | 10 ++-- test/scripts/models/category.js | 24 ++++++---- test/scripts/models/page.js | 20 ++++---- test/scripts/models/post.js | 20 ++++---- test/scripts/models/post_asset.js | 22 +++++---- test/scripts/models/tag.js | 22 +++++---- test/scripts/theme_processors/config.js | 10 ++-- 17 files changed, 243 insertions(+), 119 deletions(-) diff --git a/test/scripts/box/box.js b/test/scripts/box/box.js index c18ec645ab..44e8b06986 100644 --- a/test/scripts/box/box.js +++ b/test/scripts/box/box.js @@ -1,12 +1,12 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; var pathFn = require('path'); var fs = require('hexo-fs'); var Promise = require('bluebird'); var crypto = require('crypto'); var util = require('hexo-util'); +var sinon = require('sinon'); var Pattern = util.Pattern; var testUtil = require('../../util'); @@ -83,13 +83,17 @@ describe('Box', function(){ it('addProcessor() - no fn', function(){ var box = newBox(); + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'fn must be a function'); + }); try { box.addProcessor('test'); - assert.fail(); } catch (err){ - err.should.have.property('message', 'fn must be a function'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('_loadFiles() - create', function(){ @@ -432,13 +436,16 @@ describe('Box', function(){ it.skip('watch() - watcher has started', function(callback){ var box = newBox(); + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'Watcher has already started.'); + }); box.watch().then(function(){ - box.watch().then(function(){ - assert.fail(); - }).catch(function(err){ - err.should.have.property('message', 'Watcher has already started.'); + box.watch().catch(function(err){ + errorCallback(err); box.unwatch(); + }).finally(function() { + errorCallback.calledOnce.should.be.false; callback(); }); }); @@ -486,13 +493,17 @@ describe('Box', function(){ it('unwatch() - watcher not started', function(){ var box = newBox(); + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'Watcher hasn\'t started yet.'); + }); try { box.unwatch(); - assert.fail(); } catch (err){ - err.should.have.property('message', 'Watcher hasn\'t started yet.'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it.skip('isWatching()', function(){ diff --git a/test/scripts/extend/filter.js b/test/scripts/extend/filter.js index 839b94b978..9c601c5c0e 100644 --- a/test/scripts/extend/filter.js +++ b/test/scripts/extend/filter.js @@ -1,7 +1,6 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; var sinon = require('sinon'); describe('Filter', function(){ @@ -76,24 +75,32 @@ describe('Filter', function(){ it('unregister() - type is required', function(){ var f = new Filter(); + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'type is required'); + }); try { f.unregister(); - assert.fail(); } catch (err){ - err.should.have.property('message', 'type is required'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('unregister() - fn must be a function', function(){ var f = new Filter(); + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'fn must be a function'); + }); try { f.unregister('test'); - assert.fail(); } catch (err){ - err.should.have.property('message', 'fn must be a function'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('list()', function(){ diff --git a/test/scripts/extend/tag.js b/test/scripts/extend/tag.js index 950dfe7700..34a660b168 100644 --- a/test/scripts/extend/tag.js +++ b/test/scripts/extend/tag.js @@ -1,7 +1,7 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; +var sinon = require('sinon'); var Promise = require('bluebird'); describe('Tag', function(){ @@ -124,21 +124,31 @@ describe('Tag', function(){ }); it('register() - name is required', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'name is required'); + }); + try { tag.register(); - assert.fail(); } catch (err){ - err.should.have.property('message', 'name is required'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('register() - fn must be a function', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'fn must be a function'); + }); + try { tag.register('test'); - assert.fail(); } catch (err){ - err.should.have.property('message', 'fn must be a function'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('render() - context', function(){ diff --git a/test/scripts/filters/new_post_path.js b/test/scripts/filters/new_post_path.js index 51e2693576..3073bbd40e 100644 --- a/test/scripts/filters/new_post_path.js +++ b/test/scripts/filters/new_post_path.js @@ -1,7 +1,7 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; +var sinon = require('sinon'); var pathFn = require('path'); var moment = require('moment'); var Promise = require('bluebird'); @@ -171,10 +171,12 @@ describe('new_post_path', function(){ }); it('data is required', function(){ - return newPostPath().then(function(){ - assert.fail(); - }).catch(function(err){ + var errorCallback = sinon.spy(function(err){ err.should.have.property('message', 'Either data.path or data.slug is required!'); }); + + return newPostPath().catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; + }); }); }); \ No newline at end of file diff --git a/test/scripts/helpers/partial.js b/test/scripts/helpers/partial.js index a532e1e102..69396c667b 100644 --- a/test/scripts/helpers/partial.js +++ b/test/scripts/helpers/partial.js @@ -1,7 +1,7 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; +var sinon = require('sinon'); var pathFn = require('path'); var fs = require('hexo-fs'); var Promise = require('bluebird'); @@ -81,11 +81,16 @@ describe('partial', function(){ }); it('name must be a string', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'name must be a string!'); + }); + try { partial(); - assert.fail(); } catch (err){ - err.should.have.property('message', 'name must be a string!'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); }); \ No newline at end of file diff --git a/test/scripts/hexo/hexo.js b/test/scripts/hexo/hexo.js index d18aee7636..2f98ea0718 100644 --- a/test/scripts/hexo/hexo.js +++ b/test/scripts/hexo/hexo.js @@ -1,7 +1,6 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; var pathFn = require('path'); var fs = require('hexo-fs'); var Promise = require('bluebird'); @@ -90,11 +89,13 @@ describe('Hexo', function(){ }); it('call() - console not registered', function(){ - return hexo.call('nothing').then(function(){ - assert.fail(); - }).catch(function(err){ + var errorCallback = sinon.spy(function(err){ err.should.have.property('message', 'Console `nothing` has not been registered yet!'); }); + + return hexo.call('nothing').catch(errorCallback).finally(function(){ + errorCallback.calledOnce.should.be.true; + }); }); it('init()', function(){ diff --git a/test/scripts/hexo/locals.js b/test/scripts/hexo/locals.js index 8a1eda9813..9b51b19877 100644 --- a/test/scripts/hexo/locals.js +++ b/test/scripts/hexo/locals.js @@ -1,19 +1,24 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; +var sinon = require('sinon'); describe('Locals', function(){ var Locals = require('../../../lib/hexo/locals'); var locals = new Locals(); it('get() - name must be a string', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'name must be a string!'); + }); + try { locals.get(); - assert.fail(); } catch (err){ - err.should.have.property('message', 'name must be a string!'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('set() - function', function(){ @@ -34,21 +39,31 @@ describe('Locals', function(){ }); it('set() - name must be a string', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'name must be a string!'); + }); + try { locals.set(); - assert.fail(); } catch (err){ - err.should.have.property('message', 'name must be a string!'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('set() - value is required', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'value is required!'); + }); + try { locals.set('test'); - assert.fail(); } catch (err){ - err.should.have.property('message', 'value is required!'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('remove()', function(){ @@ -61,12 +76,17 @@ describe('Locals', function(){ }); it('remove() - name must be a string', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'name must be a string!'); + }); + try { locals.remove(); - assert.fail(); } catch (err){ - err.should.have.property('message', 'name must be a string!'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('toObject()', function(){ diff --git a/test/scripts/hexo/render.js b/test/scripts/hexo/render.js index 59a65c0e78..b9e37fea7f 100644 --- a/test/scripts/hexo/render.js +++ b/test/scripts/hexo/render.js @@ -99,10 +99,12 @@ describe('Render', function(){ }); it('render() - no path and text', function(){ - return hexo.render.render().then(function(){ - assert.fail(); - }).catch(function(err){ + var errorCallback = sinon.spy(function(err){ err.should.have.property('message', 'No input file or string!'); + }) + + return hexo.render.render().catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; }); }); @@ -222,12 +224,17 @@ describe('Render', function(){ }); it('renderSync() - no path and text', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'No input file or string!'); + }); + try { hexo.render.renderSync(); - assert.fail(); } catch (err){ - err.should.have.property('message', 'No input file or string!'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('renderSync() - options', function(){ diff --git a/test/scripts/hexo/router.js b/test/scripts/hexo/router.js index 900a7def9d..ea1d6bbc47 100644 --- a/test/scripts/hexo/router.js +++ b/test/scripts/hexo/router.js @@ -1,7 +1,6 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; var Promise = require('bluebird'); var Readable = require('stream').Readable; var pathFn = require('path'); @@ -59,12 +58,17 @@ describe('Router', function(){ }); it('format() - path must be a string', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'path must be a string!'); + }) + try { router.format(function(){}); - assert.fail(); } catch (err){ - err.should.have.property('message', 'path must be a string!'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }) it('set() - string', function(){ @@ -121,21 +125,31 @@ describe('Router', function(){ }); it('set() - path must be a string', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'path must be a string!'); + }); + try { router.set(); - assert.fail(); } catch (err){ - err.should.have.property('message', 'path must be a string!'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('set() - data is required', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'data is required!'); + }); + try { router.set('test'); - assert.fail(); } catch (err){ - err.should.have.property('message', 'data is required!'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('get() - error handling', function(){ @@ -143,11 +157,13 @@ describe('Router', function(){ throw new Error('error test'); }); - return testUtil.stream.read(router.get('test')).then(function(){ - assert.fail(); - }).catch(function(err){ + var errorCallback = sinon.spy(function(err){ err.should.have.property('message', 'error test'); }); + + return testUtil.stream.read(router.get('test')).catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; + }); }); it('get() - no data', function(){ @@ -185,12 +201,17 @@ describe('Router', function(){ }); it('get() - path must be a string', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'path must be a string!'); + }); + try { router.get(); - assert.fail(); } catch (err){ - err.should.have.property('message', 'path must be a string!'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('get() - export stringified JSON object', function(){ @@ -220,12 +241,17 @@ describe('Router', function(){ }); it('isModified() - path must be a string', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'path must be a string!'); + }); + try { router.isModified(); - assert.fail(); } catch (err){ - err.should.have.property('message', 'path must be a string!'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); it('remove()', function(){ @@ -242,11 +268,16 @@ describe('Router', function(){ }); it('remove() - path must be a string', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'path must be a string!'); + }); + try { router.remove(); - assert.fail(); } catch (err){ - err.should.have.property('message', 'path must be a string!'); + errorCallback(err); } + + errorCallback.calledOnce.should.be.true; }); }); \ No newline at end of file diff --git a/test/scripts/models/asset.js b/test/scripts/models/asset.js index 7e95f65678..edbca65b75 100644 --- a/test/scripts/models/asset.js +++ b/test/scripts/models/asset.js @@ -1,7 +1,7 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; +var sinon = require('sinon'); var pathFn = require('path'); describe('Asset', function(){ @@ -20,20 +20,24 @@ describe('Asset', function(){ }); it('_id - required', function(){ - return Asset.insert({}).then(function(){ - assert.fail(); - }).catch(function(err){ + var errorCallback = sinon.spy(function(err) { err.should.have.property('message', 'ID is not defined'); }); + + return Asset.insert({}).catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; + }); }); it('path - required', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', '`path` is required!'); + }); + return Asset.insert({ _id: 'foo' - }).then(function(){ - assert.fail(); - }).catch(function(err){ - err.should.have.property('message', '`path` is required!'); + }).catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; }); }); diff --git a/test/scripts/models/cache.js b/test/scripts/models/cache.js index cb3ebe5dfe..1bff141236 100644 --- a/test/scripts/models/cache.js +++ b/test/scripts/models/cache.js @@ -1,7 +1,7 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; +var sinon = require('sinon'); describe('Cache', function(){ var Hexo = require('../../../lib/hexo'); @@ -9,10 +9,12 @@ describe('Cache', function(){ var Cache = hexo.model('Cache'); it('_id - required', function(){ - return Cache.insert({}).then(function(){ - assert.fail(); - }).catch(function(err){ + var errorCallback = sinon.spy(function(err) { err.should.have.property('message', 'ID is not defined'); }); + + return Cache.insert({}).catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; + }); }); }); \ No newline at end of file diff --git a/test/scripts/models/category.js b/test/scripts/models/category.js index 9dd40cd01d..c468b47ef5 100644 --- a/test/scripts/models/category.js +++ b/test/scripts/models/category.js @@ -1,7 +1,7 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; +var sinon = require('sinon'); var Promise = require('bluebird'); describe('Category', function(){ @@ -16,11 +16,13 @@ describe('Category', function(){ }); it('name - required', function(){ - return Category.insert({}).then(function(){ - assert.fail(); - }).catch(function(err){ + var errorCallback = sinon.spy(function(err) { err.should.have.property('message', '`name` is required!'); }); + + return Category.insert({}).catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; + }); }); it.skip('parent - reference'); @@ -212,18 +214,22 @@ describe('Category', function(){ }); it('check whether a category exists', function(){ + var errorCallback = sinon.spy(function(err){ + err.should.have.property('message', 'Category `foo` has already existed!'); + }); + return Category.insert({ name: 'foo' }).then(function(data){ + var errorCall + Category.insert({ name: 'foo' - }).then(function(){ - assert.fail(); - }).catch(function(err){ - err.should.have.property('message', 'Category `foo` has already existed!'); - }); + }).catch(errorCallback); return Category.removeById(data._id); + }).finally(function() { + errorCallback.calledOnce.should.be.true; }); }); diff --git a/test/scripts/models/page.js b/test/scripts/models/page.js index 99471998d4..1675c8642d 100644 --- a/test/scripts/models/page.js +++ b/test/scripts/models/page.js @@ -1,7 +1,7 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; +var sinon = require('sinon'); var pathFn = require('path'); describe('Page', function(){ @@ -32,20 +32,24 @@ describe('Page', function(){ }); it('source - required', function(){ - return Page.insert({}).then(function(){ - assert.fail(); - }).catch(function(err){ + var errorCallback = sinon.spy(function(err) { err.should.have.property('message', '`source` is required!'); }); + + return Page.insert({}).catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; + }); }); it('path - required', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', '`path` is required!'); + }); + return Page.insert({ source: 'foo' - }).then(function(){ - assert.fail(); - }).catch(function(err){ - err.should.have.property('message', '`path` is required!'); + }).catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; }); }); diff --git a/test/scripts/models/post.js b/test/scripts/models/post.js index ba237e447d..525af4d6c4 100644 --- a/test/scripts/models/post.js +++ b/test/scripts/models/post.js @@ -1,7 +1,7 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; +var sinon = require('sinon'); var pathFn = require('path'); var Promise = require('bluebird'); @@ -45,20 +45,24 @@ describe('Post', function(){ }); it('source - required', function(){ - return Post.insert({}).then(function(){ - assert.fail(); - }).catch(function(err){ + var errorCallback = sinon.spy(function(err){ err.should.have.property('message', '`source` is required!'); }); + + return Post.insert({}).catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; + }); }); it('slug - required', function(){ + var errorCallback = sinon.spy(function(err){ + err.should.have.property('message', '`slug` is required!'); + }); + return Post.insert({ source: 'foo.md' - }).then(function(){ - assert.fail(); - }).catch(function(err){ - err.should.have.property('message', '`slug` is required!'); + }).catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; }); }); diff --git a/test/scripts/models/post_asset.js b/test/scripts/models/post_asset.js index f58f069f47..1e2603eabe 100644 --- a/test/scripts/models/post_asset.js +++ b/test/scripts/models/post_asset.js @@ -1,7 +1,7 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; +var sinon = require('sinon'); var url = require('url'); var pathFn = require('path'); @@ -35,21 +35,25 @@ describe('PostAsset', function(){ }); it('_id - required', function(){ - return PostAsset.insert({}).then(function(){ - assert.fail(); - }).catch(function(err){ + var errorCallback = sinon.spy(function(err) { err.should.have.property('message', 'ID is not defined'); }); + + return PostAsset.insert({}).catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; + });; }); it('slug - required', function(){ - return PostAsset.insert({ - _id: 'foo' - }).then(function(){ - assert.fail(); - }).catch(function(err){ + var errorCallback = sinon.spy(function(err) { err.should.have.property('message', '`slug` is required!'); }); + + return PostAsset.insert({ + _id: 'foo' + }).catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; + });; }); it('path - virtual', function(){ diff --git a/test/scripts/models/tag.js b/test/scripts/models/tag.js index 114abbdc61..1a3a0de06e 100644 --- a/test/scripts/models/tag.js +++ b/test/scripts/models/tag.js @@ -1,7 +1,7 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; +var sinon = require('sinon'); var Promise = require('bluebird'); var _ = require('lodash'); @@ -17,11 +17,13 @@ describe('Tag', function(){ }); it('name - required', function(){ - return Tag.insert({}).then(function(){ - assert.fail(); - }).catch(function(err){ + var errorCallback = sinon.spy(function(err) { err.should.have.property('message', '`name` is required!'); }); + + return Tag.insert({}).catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; + }); }); it('slug - virtual', function(){ @@ -194,18 +196,20 @@ describe('Tag', function(){ }); it('check whether a tag exists', function(){ + var errorCallback = sinon.spy(function(err) { + err.should.have.property('message', 'Tag `foo` has already existed!'); + }); + return Tag.insert({ name: 'foo' }).then(function(data){ Tag.insert({ name: 'foo' - }).then(function(){ - assert.fail(); - }).catch(function(err){ - err.should.have.property('message', 'Tag `foo` has already existed!'); - }); + }).catch(errorCallback); return Tag.removeById(data._id); + }).finally(function() { + errorCallback.calledOnce.should.be.true; }); }); diff --git a/test/scripts/theme_processors/config.js b/test/scripts/theme_processors/config.js index c6fa716e69..bcf5e3094b 100644 --- a/test/scripts/theme_processors/config.js +++ b/test/scripts/theme_processors/config.js @@ -1,7 +1,7 @@ 'use strict'; var should = require('chai').should(); -var assert = require('chai').assert; +var sinon = require('sinon'); var pathFn = require('path'); var fs = require('hexo-fs'); var Promise = require('bluebird'); @@ -83,10 +83,12 @@ describe('config', function(){ type: 'create' }); - return process(file).then(function(){ - assert.fail(); - }).catch(function(err){ + var errorCallback = sinon.spy(function(err) { err.should.have.property('message', 'Theme config load failed.'); + }); + + return process(file).catch(errorCallback).finally(function() { + errorCallback.calledOnce.should.be.true; }).catch(function(){}); // Catch again because it throws error }); }); \ No newline at end of file