Skip to content

Commit

Permalink
Change assert.fail() to using sinon.spy()
Browse files Browse the repository at this point in the history
  • Loading branch information
naokiy committed Apr 15, 2015
1 parent f655f59 commit 3e84730
Show file tree
Hide file tree
Showing 17 changed files with 243 additions and 119 deletions.
29 changes: 20 additions & 9 deletions test/scripts/box/box.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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();
});
});
Expand Down Expand Up @@ -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(){
Expand Down
17 changes: 12 additions & 5 deletions test/scripts/extend/filter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

var should = require('chai').should();
var assert = require('chai').assert;
var sinon = require('sinon');

describe('Filter', function(){
Expand Down Expand Up @@ -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(){
Expand Down
20 changes: 15 additions & 5 deletions test/scripts/extend/tag.js
Original file line number Diff line number Diff line change
@@ -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(){
Expand Down Expand Up @@ -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(){
Expand Down
10 changes: 6 additions & 4 deletions test/scripts/filters/new_post_path.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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;
});
});
});
11 changes: 8 additions & 3 deletions test/scripts/helpers/partial.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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;
});
});
9 changes: 5 additions & 4 deletions test/scripts/hexo/hexo.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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(){
Expand Down
38 changes: 29 additions & 9 deletions test/scripts/hexo/locals.js
Original file line number Diff line number Diff line change
@@ -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(){
Expand All @@ -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(){
Expand All @@ -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(){
Expand Down
17 changes: 12 additions & 5 deletions test/scripts/hexo/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});

Expand Down Expand Up @@ -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(){
Expand Down
Loading

0 comments on commit 3e84730

Please sign in to comment.