Skip to content

Commit

Permalink
refactor test with sinion stub
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyelee committed Jun 22, 2015
1 parent f68ab32 commit 93c5321
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions test/test.command.allowUnknownOption.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,58 @@
/**
* Module dependencies.
*/

var util = require('util');
var program = require('../')
, should = require('should');
, sinon = require('sinon').sandbox.create()
, should = require('should');

var oldProcessExit = process.exit;
var oldConsoleError = console.error;
var stubError = sinon.stub(console, 'error');
var stubExit = sinon.stub(process, 'exit');

program
.version('0.0.1')
.option('-p, --pepper', 'add pepper');
program.parse('node test -m'.split(' '));

var consoleErrors = [];
process.exit = function () {
};
console.error = function () {
consoleErrors.push(util.format.apply(util, arguments));
};
stubError.callCount.should.equal(3);

program.parse('node test -m'.split(' '));
consoleErrors.length.should.equal(3);

// test subcommand
var consoleErrors = [];
resetStubStatus();
program
.command('sub')
.action(function () {
});
program.parse('node test sub -m'.split(' '));
consoleErrors.length.should.equal(3);

consoleErrors = [];
stubError.callCount.should.equal(3);
stubExit.calledOnce.should.be.true();

// command with `allowUnknownOption`
resetStubStatus();
program
.version('0.0.1')
.option('-p, --pepper', 'add pepper');

program
.allowUnknownOption()
.parse('node test -m'.split(' '));
consoleErrors.length.should.equal(0);

// test subcommand
var consoleErrors = [];
stubError.callCount.should.equal(0);
stubExit.calledOnce.should.be.false();

// subcommand with `allowUnknownOption`
resetStubStatus();
program
.command('sub')
.command('sub2')
.allowUnknownOption()
.action(function () {
});
program.parse('node test sub -m'.split(' '));
consoleErrors.length.should.equal(3);
program.parse('node test sub2 -m'.split(' '));

stubError.callCount.should.equal(0);
stubExit.calledOnce.should.be.false();

process.exit = oldProcessExit;
console.error = oldConsoleError;

function resetStubStatus() {
stubError.reset();
stubExit.reset();
}

0 comments on commit 93c5321

Please sign in to comment.