views:

150

answers:

3

doh is the dojo unit-testing framework. I am trying to use doh to test a non-dojo javascript code, but i am facing the problem that doh seems intrusive and oblige me to use dojo.provide() in the tested js file(and the corresponding dojo.require() in the test js file). I want the tested js file to be unmodified and dojo-agnostic. Is it possible ?

A: 

I think DOH does have a dependency on the Dojo loader (only). Have you tried just defining the object which would normally be in the dojo.provide? You might get away with that. Instead of doing

dojo.provide("mytests.mymodule")

try

mytests.mymodule={};
peller
A: 

I know this doesn't directly answer your question since you are specifically asking about Dojo, but I thought I'd just post this answer anyway. The example uses Google Closure as the test runner, but the tests aren't obliged to use Closure in the tests functions. The Closure test runner can also use JsUnit tests. If it isn't helpful the first section of the DOH user manual gives a little hint.

D.O.H. has no Dojo dependencies...

It looks to me that a test function passed to DOH can be any function?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>js unit test example</title>
<script src="git.svnco/closure-library-read-only/closure/goog/base.js"></script>
<script> 
    goog.require("goog.testing.TestRunner");
    goog.require("goog.testing.TestCase");
    goog.require("goog.testing.TestCase.Test");
</script>
</head>
<body>
<script>
   // plain functions, using JsUnit compatible functions
   function myFirstTest() {
      assertEquals("something", "something");
   }

   function mySecondTest() {
      assertEquals("will", "fail");
   }


   var testRunner = new goog.testing.TestRunner();

   var testCase = new goog.testing.TestCase();
   var test = new goog.testing.TestCase.Test("myFirstTest", myFirstTest);
   testCase.add(test);
   test = new goog.testing.TestCase.Test("mySecondTest", mySecondTest);
   testCase.add(test);

   testRunner.initialize(testCase);
   testRunner.execute();    
</script>
</body>
</html>

Closure can also be set up to auto-detect tests, and has a nice Component called MultiTestRunner which the Closure Library uses to run all tests on it self.

neshaug
+1  A: 

I have found the solution.

  • simple/MyModule.js
  • simple/tests/MyModuleTest.js

In the test file, just use:

dojo.provide("simple.tests.MyModuleTest");

dojo.require("doh.runner");

dojo.require("simple.MyModule",true);

as the js file is find by its name without the dojo.provide() module check

http://api.dojotoolkit.org/jsdoc/1.2/dojo.require

François Wauquier