I have written a JavaScript function at C:\alttestrunner\spec\app\mythingtotest.js which I was able to get under mocha testing WITHOUT using Node.js by way of using a HTML test runner. My function is:
define([], function(){
return {
pythagoreanTheorem: function(a,b){
var aSquared = a*a;
var bSquared = b*b;
var cSquared = aSquared + bSquared;
var c = Math.sqrt(cSquared);
return c;
}
};
});
The HTML test runner at C:\alttestrunner\test.html is just a plain jane .html document that one may open up outside of any web server!
The guts of the HTML file are shown below. Note the dependencies on require.js, mocha, and Chai. You will need to go get these from the world at large to get this example to work.
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<script type="text/javascript" src="require.js"></script>
<script type="text/javascript">
require([
'require',
'./lib/chai',
'./lib/mocha'
], function (require, chai, moca) {
var options = {
ui: 'bdd',
timeout: 10000
};
mocha.setup(options);
window.assert = chai.assert;
require([
'spec/alltests'
], function () {
mocha.run();
});
});
</script>
</head>
<body>
<div id="mocha"></div>
</body>
</html>
There are only two more files to discuss. Firstly, C:\alttestrunner\spec\alltests.js denotes all test suites to run:
define([
"./mytestsuite"
], function () {
return "all tests loaded";
});
As you can see, we could run numerous test suites, but we are only running one. It is kept at C:\alttestrunner\spec\mytestsuite.js and looks like so:
define(["./app/mythingtotest"], function (mythingtotest) {
"use strict";
describe("attempting tests", function () {
describe("test my thing to test", function () {
describe("test Pythagorean Theorem", function () {
var three;
var four;
var five;
var twelve;
var thirteen;
before(function () {
three = 3;
four = 4;
five = 5;
twelve = 12;
thirteen = 13;
});
it("should give a hypotenuse five units long for a right angle with sides three and
four units long", function () {
var hypotenuse = mythingtotest.pythagoreanTheorem(three,four);
assert.equal(hypotenuse, five);
});
it("should give a hypotenuse thirteen units long for a right angle with sides five and
twelve units long", function () {
var hypotenuse = mythingtotest.pythagoreanTheorem(five,twelve);
assert.equal(hypotenuse, thirteen);
});
});
});
});
});
No comments:
Post a Comment