|
-
Mar 4th, 2004, 11:13 PM
#1
Thread Starter
Hyperactive Member
JsUnit -- test driven javascript? you bet!
Ok, so to preface this post: I HATE JAVASCRIPT. If I never have to code javascript again it'll be too soon. Long story short, one of our front end guys built a modal dialog nightmare...er..i mean solution. Bottom line, if anything goes wrong with that code it's a frickin mess, even though it's javascript, it's so messy it's as good as a black box component. I'd rather not have to open it up.
Well, today we had a problem in that code and it was buried deep. I went nuts tryin to find the error and still haven't found it; it's not a show stopper so it can wait until tomorrow. I got to thinkin though. If every method of the way was thoroughly tested, we wouldn't have to do much guess work as to where the problem was...cuz there wouldn't be a problem to begin with. So, before rolling my own testing javascript testing framework(i really didn't want to, cuz, have i mentioned that, I HATE JAVASCRIPT? anyway...), I set out on a google journey to find an existing one. Found one in about a second. JsUnit. I just loaded it up to take it for a test drive. Way easy. I downloaded the code from JsUnit. Wrote a stupid hello world type test in about a second, and had 'tested javascript code' - right out of the box. Anywho, no installation necessary, just download and extract the files from the zip,here's my stupid employee example(if it's not obvious, all of the tests are prefixed with 'test'):
PHP Code:
<html>
<head>
<script language="JavaScript" type="text/javascript" src="../app/jsUnitCore.js"></script>
<script language="JavaScript">
function Employee( firstName, lastName )
{
this.FirstName = firstName;
this.LastName = lastName;
this.FullName = getFullName;
}
function getFullName()
{
if( ( this.LastName == undefined || this.LastName.length == 0 ) ||
( this.FirstName == undefined || this.FirstName.length == 0 ) )
{
return undefined
}
else
{
return this.LastName + ", " + this.FirstName;
}
}
function testEmployeeFirstName()
{
emp = new Employee( "Chris" );
assertNotNull( "emp should not be null", emp );
assertEquals( "FirstName should match the constructor", "Chris", emp.FirstName );
}
function testEmployeeLastName()
{
emp = new Employee ( "Chris", "Carter" );
assertNotNull( "emp should not be null", emp );
assertEquals( "LastName should match constructor", "Carter", emp.LastName );
}
function testEmployeeFullName()
{
emp = new Employee ( "Chris", "Carter" );
assertNotNull( "emp should not be null", emp );
assertEquals( "FullName() should be 'LastName, FirstName'", "Carter, Chris", emp.FullName() );
}
function testEmployeeExpectUndefined_getFullName()
{
assertUndefined( "getFullName() should return undefined if called directly", getFullName() );
}
</script>
<body>
<form>
<input type="button" onclick="init()" value="Employee">
</form>
</body>
</html>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|