Created
September 20, 2017 21:41
-
-
Save aadennis/1b1801a788cf372c2509f0650b1b1e69 to your computer and use it in GitHub Desktop.
Basic node testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Next is dennis-rules.js... | |
// Example call: dennisRules.applyRule(currName, results); | |
// The function applyRule always "expects" [v] of 'dennis'. | |
// if [v] is not 'dennis', then it follows convention by populating the callback with an | |
// Error object as the first argument, and leaving the other positions er undefined. | |
// Else ([v] IS 'dennis'), it leaves the first argument as null (again, following convention), | |
// and populates argument 2 with the input [v] value, and argument 3 with the random period to | |
// wait before returning. | |
// For the function to be available to callers, we call module.exports.(function name) = (function name); | |
var maxTime = 1000; | |
var applyRule = function (v, callback) { | |
var waitTime = Math.floor(Math.random() * (maxTime + 1)); | |
if (v != 'dennis') { | |
setTimeout(function () { | |
callback(new Error("Expected [dennis], got [" + v + "]")); | |
}, waitTime); | |
} else { | |
setTimeout(function () { | |
callback(null, v, waitTime); | |
}, waitTime); | |
} | |
}; | |
module.exports.applyRule = applyRule; | |
Next is run-dennis-rules.js | |
// Example call from e.g. PowerShell: | |
// node .\run-dennis-rules.js | |
// See dennis-rules.js for the functionality under test | |
var currName = 'dennis'; | |
var dennisRules = require('./dennis-rules'); | |
var assert = require('assert'); | |
console.log("Calling 'dennis-rules' with " + currName); | |
// here, we will just be reporting back on the results returned from applyRule... | |
// but not doing any concrete testing... | |
var resultsType1 = function(err, myresults, time) { | |
if (err) { | |
console.log("ERROR: " + err.message); | |
} else { | |
console.log("Results: " + myresults + " " + time + "(the end)"); | |
} | |
}; | |
// plain old call to applyRule... | |
console.log("Plain old call..."); | |
dennisRules.applyRule(currName, resultsType1); | |
// For the most basic of "proper" testing, we need to trap the content of the callback. | |
// So first, make a call to applyRule which uses an anonymous function to test for (positive) results... | |
// That is, 'dennis' was expected, and 'dennis' we got... | |
console.log("Test - positive..."); | |
dennisRules.applyRule(currName, function(err, resultsTestingScope1) { | |
assert.ifError(err, "Expected no error, but Error is populated."); | |
assert.equal('dennis', resultsTestingScope1, "some stuff is not quite right 1 "); | |
}); | |
// call to applyRule which uses an anonymous function to test for the error object being populated, i.e. the test | |
// was expected to fail, and it did/should fail... | |
// That is, 'dennis' was expected, but we got something that is not 'dennis'. | |
// We do not test for arguments 2 and 3, because if argument 1 has a non-null value, then reviewing the FUT tells us | |
// that all subsequent arguments are disregarded / not populated. | |
console.log("Test - negative..."); | |
dennisRules.applyRule('Adam', function(err, resultsTestingScope2) { | |
assert.notEqual(null, err, "Expected an error, but got no error"); | |
assert.equal('Expected [dennis], got [Adam]', err.message); | |
}); | |
assert.notEqual(1,2); | |
assert.equal(1,1); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment