1

Is there any restrictions on the name of the test function?

When I write the following as testA() and testB(), both of them will be implemented.

AjaxCreateTest = TestCase("AjaxCreateTest");  
AjaxCreateTest.prototype.testA = function(){};  
AjaxCreateTest.prototype.testb = function(){};

But if I change the name from "testB" to "AjaxCreateT", just testA() will be implemented. So weird. Could someone help?

Jasonw
  • 5,054
  • 7
  • 43
  • 48

1 Answers1

3

JsTestDriver only executes methods that start with the prefix "test". This is a naming convention to enable you to write helper methods on the same object that aren't executed as tests.

There are also two reserved method names setUp and tearDown, which will be executed before and after each test respectively.

You can even use spaces in your test names, which makes your tests nicely readable, for example:

TestCase("AjaxCreateTest", {
    setUp : function() {
        this.subject = new MyAjaxCode();
        this.stubXHR();
    },

    tearDown : function() {
        this.restoreXHR();
    },

    stubXHR : function() {
        // stub global objects to intercept Ajax calls
    },

    restoreXHR : function() {
        // restore global state
    },

    "test should say hi" : function() {
        assertEquals("Hi", this.subject.sayHi());
    }
}

In the example above there's only one test, the rest are helper methods.

I can recommend Sinon.JS for helping you stub your Ajax calls (see "Fake XHR") and for further examples.

meyertee
  • 2,211
  • 17
  • 16