Section 11.6. JavaScript Unit Testing


11.6. JavaScript Unit Testing

Defined in unittest.js, which is not included in the default Rails skeleton. unittest.js provides tools to support JavaScript unit testing. The main interface is provided with the Test.Unit.Runner, a utility class for writing unit test cases. Tests are written in JavaScript and run inside the browser.


initialize( testcases[, options] )

Constructor for a new test runner instance. The testcases argument is an object of functions that will be run for the test. Each test case name should start with test. You can also define two additional functions, setup and teardown, which will be run before and after each test case. For example:

new Test.Unit.Runner({   // optional setup function, run before each individual test case   setup: function(  ) { with(this) {     // code   }},   // optional teardown function, run after each individual test case   teardown: function(  ) { with(this) {     // code   }},   // test cases follow, each method which starts    // with "test" is considered a test case   testATest: function(  ) { with(this) {     // code   }},   testAnotherTest: function(  ) { with(this) {     // code   }} });

The optional second argument for the constructor is an options object. Its keys can include:

testLog

Specifies the ID of the element that will be sent the test output. Defaults to testlog.
test

If specified, only the given test case will run.


Tests are created in a page template that looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html>   <head>     <title>JavaScript Unit Test</title>     <script src="/books/4/386/1/html/2/prototype.js" type="text/javascript"></script>     <script src="/books/4/386/1/html/2/unittest.js" type="text/javascript"></script>     <!-- Other JavaScript includes needed for tests -->     <link href="test.css" type="text/css" />   </head>   <body>     <h1>JavaScript Unit Test</h1>     <!-- Log output -->     <div > </div>     <!-- Sandbox -->     <div > </div>     <!-- Tests -->     <script type="text/javascript">       new Test.Unit.Runner({         // tests       });     </script>   </body> </html>

The sandbox element can contain any HTML markup needed by the test cases. The results of a test run can be reported back to the server, by adding a resultsURL query parameter to the test template URL, e.g., http://localhost:3000/test/js_unit_test.html?resultsURL=/log_test_results.

11.6.1. Assertions

The basic call to an assertion within a test method in Test.Unit.Runner looks as follows:

testExample: function(  ) { with(this) {   var myElement = $('mydiv');   assertEqual("DIV", myElement.tagName);   assertEqual("DIV", myElement.tagName, "Hmm, not a DIV?"); }};

All assertions take an optional message as last parameter, which is used in case of assertions failure for additional log remarks.


assert( expression [, message ])

Asserts that expression evaluates to true.


assertEqual(expected, actual[, message ])

Asserts that expected and actual are equal.


assertNotEqual(expected, actual[, message ])

Asserts that expected and actual are not equal.


assertNull(object[, message ])

Asserts that object is null.


assertNotNull(object[, message ])

Asserts that object is not null.


assertHidden(element[, message ])

Asserts that element's display property is none.


assertVisible(element[, message ])

Asserts that element is visible (that it and all its ancestors are not display: none).


assertNotVisible(element[, message ])

Asserts that element (or one of its ancestors) are display: none.


assertInstanceOf(object, expected[, message ])

Asserts that object is an instance of the type expected.


assertNotInstanceOf(object, expected[, message ])

Asserts that object is not an instance of the type expected.


assertEnumEqual(expected, actual[, message ])

Asserts that all the members of the actual collection match the members of expected.




Ajax on Rails
Ajax on Rails
ISBN: 0596527446
EAN: 2147483647
Year: 2006
Pages: 103
Authors: Scott Raymond

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net