From ea000ead66f0a809c0d7768202185598f7595f8b Mon Sep 17 00:00:00 2001 From: timmycheng1221 Date: Wed, 28 Oct 2015 23:52:37 +0800 Subject: [PATCH 1/4] 6 Write at least one passing test (PR green light) --- calculator.js | 1 + test/test_calculator.js | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/calculator.js b/calculator.js index caf34db..18d6383 100644 --- a/calculator.js +++ b/calculator.js @@ -12,4 +12,5 @@ function substract(num1, num2){ } module.exports.add = add; +module.exports.substract = substract; module.exports.isNumber = isNumber; diff --git a/test/test_calculator.js b/test/test_calculator.js index 13f6205..84fecab 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -7,4 +7,14 @@ describe('Calculator', function(){ assert.equal('2', cal.add(1, 1)); }) }) + describe('substract', function(){ + it('substract', function(){ + assert.equal('0', cal.substract(1, 1)); + }) + }) + describe('isNumber', function(){ + it('isNumber', function(){ + assert.equal(true, cal.isNumber(1)); + }) + }) }) From b62c64afd9cf290dd51a8f28985b48d7d1e89446 Mon Sep 17 00:00:00 2001 From: timmycheng1221 Date: Wed, 28 Oct 2015 23:58:02 +0800 Subject: [PATCH 2/4] Add multiple feature and test --- calculator.js | 5 +++++ test/test_calculator.js | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/calculator.js b/calculator.js index 18d6383..cf799f9 100644 --- a/calculator.js +++ b/calculator.js @@ -11,6 +11,11 @@ function substract(num1, num2){ return num1 - num2; } +function multiple(num1, num2){ + return num1 * num2; +} + module.exports.add = add; module.exports.substract = substract; +module.exports.multiple = multiple; module.exports.isNumber = isNumber; diff --git a/test/test_calculator.js b/test/test_calculator.js index 84fecab..7700e52 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -12,6 +12,11 @@ describe('Calculator', function(){ assert.equal('0', cal.substract(1, 1)); }) }) + describe('multiple', function(){ + it('multiple', function(){ + assert.equal('9', cal.multiple(3, 3)); + }) + }) describe('isNumber', function(){ it('isNumber', function(){ assert.equal(true, cal.isNumber(1)); From 160cc45ee927155ff7235f1beab4624f66e40100 Mon Sep 17 00:00:00 2001 From: timmycheng1221 Date: Thu, 29 Oct 2015 00:01:03 +0800 Subject: [PATCH 3/4] Add negative path tests for isNumber function --- test/test_calculator.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/test_calculator.js b/test/test_calculator.js index 7700e52..a6e92d3 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -19,7 +19,12 @@ describe('Calculator', function(){ }) describe('isNumber', function(){ it('isNumber', function(){ - assert.equal(true, cal.isNumber(1)); + assert.equal(true, cal.isNumber('1')); + }) + }) + describe('isNotNumber', function(){ + it('isNotNumber', function(){ + assert.equal(false, cal.isNumber('1a')); }) }) }) From 4569bb234fd02a78b70cf2095b1a81f18e60a75b Mon Sep 17 00:00:00 2001 From: timmycheng1221 Date: Thu, 29 Oct 2015 01:08:03 +0800 Subject: [PATCH 4/4] Stubbing with Sinon.js(random number) --- calculator.js | 5 +++++ package.json | 1 + test/test_calculator.js | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/calculator.js b/calculator.js index cf799f9..f7ae484 100644 --- a/calculator.js +++ b/calculator.js @@ -15,7 +15,12 @@ function multiple(num1, num2){ return num1 * num2; } +function randomSubstract(num){ + return num - Math.random() ; +} + module.exports.add = add; module.exports.substract = substract; module.exports.multiple = multiple; module.exports.isNumber = isNumber; +module.exports.randomSubstract = randomSubstract; diff --git a/package.json b/package.json index d727f6d..b723763 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "license": "MPLv2", "dependencies": { "mocha": "~2.3.2" + "sinon": "~1.17.2" }, "repository":{} } diff --git a/test/test_calculator.js b/test/test_calculator.js index a6e92d3..53c3f52 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -1,4 +1,5 @@ var assert = require('assert'); +var sinon = require('sinon'); var cal = require('../calculator.js') describe('Calculator', function(){ @@ -27,4 +28,10 @@ describe('Calculator', function(){ assert.equal(false, cal.isNumber('1a')); }) }) + describe('randomSubstract', function(){ + it('randomSubstract', function(){ + var cb = sinon.stub(Math, 'random').returns(0.1); + assert.equal(0.9, cal.randomSubstract(1)); + }) + }) })