Sunday, August 10, 2014

Using Dojo DOH

D.O.H.: Dojo Objective Harness

DOH is...

  • used to unit test JavaScript functions and custom widgets
  • Runs in many environments from browsers to JS runtime environments such as Rhino, node.js

Where is DOH located?

  • Download Dojo source code (you get dojo, dijit, dojox, util packages)
  • DOH is located in util package.

Give an example of running a doh test from browser?

  • Write a unit test file (mysampletest.js)
define(["doh", "../date"], function(doh, date){ //I am trying to test the functionality of date module
    doh.register("tests.date.util", [ //tests.date.util is the name of the test group
        function test_date_getDaysInMonth(t){//test_date_getDaysInMonth - name of the test
            // months other than February
            t.is(31, date.getDaysInMonth(new Date(2006,0,1))); // returns true

            // Februarys
            t.is(28, date.getDaysInMonth(new Date(2006,1,1))); // returns true

        }
    ]);
});
  • Save this file under dojo/tests (if you are testing some modules in a package, save the unit test file in that package under tests folder - in this example, I am trying to test the date module in dojo package, so I am going to save it under dojo/tests folder)
  • Run it from your local browser
http://localhost:8080/js/dojo-src/util/doh/runner.html?test=dojo/tests/mysampletest


Suppose if I have more than one unit test file (for example mysampletest.js & mysampletest2.js) how can I provide those files to doh?

  • Save both the files under dojo/tests
  • mysampletest.js
define(["doh", "../date"], function(doh, date){ //I am trying to test the functionality of date module
    doh.register("tests.date.util", [ //tests.date.util is the name of the test group
        function test_date_getDaysInMonth(t){ //test_date_getDaysInMonth is the name of the test
            // months other than February
            t.is(31, date.getDaysInMonth(new Date(2006,0,1))); // returns true

            // Februarys
            t.is(28, date.getDaysInMonth(new Date(2006,1,1))); // returns true

        }
    ]);
});
  • mysampletest2.js
define(["doh", "dojo/_base/array"], function(doh, array){ //I am trying to test the functionality of array module

    doh.register("tests._base.array", [//this is the name of my test group
        function testIndexOf(t){ //name of my test
            var foo = [128, 256, 512];
            t.assertEqual(1, array.indexOf([45, 56, 85], 56));
            t.assertTrue(
                array.some(foo, function(elt, idx, array){
                    t.assertEqual(3, array.length);
                    return true;
                })
            );
        }
    ]);
});
  • Create a module file, mysampletests.js as below and save it under dojo/tests
define([ //Module that helps loading of unit test files
    "dojo/tests/mysampletest",
    "dojo/tests/mysampletest2"
], 1);
  • Run the unit tests by providing the module to the doh:
http://localhost:8080/js/dojo-src/util/doh/runner.html?test=dojo/tests/mysampletests

Best references to understand more about D.O.H:

By the way, what is the meaning of assert?

as·sert
əˈsərt/
verb
  1. state a fact or belief confidently and forcefully.
    "the company asserts that the cuts will not affect development"
    • cause others to recognize (one's authority or a right) by confident and forceful behavior.
      "the good librarian is able to assert authority when required"
      synonyms:insist on, stand up for, upholddefendcontendestablish, press for,push for, stress More
    • behave or speak in a confident and forceful manner.
      "it was time to assert himself"
      synonyms:behave confidently, speak confidently, be assertive, put oneself forward,take a stand, make one's presence felt;



No comments:

Post a Comment