Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Unit Test for JavaScript

, 14 Dec 2007 CPOL
How to strengthen your client-side code and improve reliability
unitjs.zip
UnitJS
green.gif
red.gif
<html>
    <head>
        <title>Unit.js</title>
        <!-- JavaScript Debugging Tool @ http://www.codeproject.com/KB/scripting/Javascript_Debugging_Tool.aspx -->
        <script type="text/javascript" src="debugHelper.js"></script>
        <link type="text/css" rel="Stylesheet" href="debugHelper.css" />
        <!-- Unit.js Library - Javascript Unit Testing Library @ http://www.rationalcode.com/ -->
        <script type="text/javascript" src="unit.js"></script>
    </head>
    <body>
        <script type="text/javascript" defer="defer">
            //Component to be tested
			function _GuidComponent()
			{
				// Uniqueidentifier seed
				this.GUID = 0;
			}
			// Uniqueidentifier generator
			_GuidComponent.prototype.GetNew = function()
			{
				return "o_" + this.GUID++;
			}
			var guidComponent = new _GuidComponent();
        </script>
        
        <script type="text/javascript" defer="defer">
			//Text Fixture
	        function GuidTest()
	        {
	            this.Description = "Unit tests to exercise Guid component";
	            this.Category = "Helpers";
	        }
	        //Javascript inheritance. It is similar to implementing a interface.
	        GuidTest.prototype = new ITextFixture();
	        //Javascript hack to maintain the correct constructor
	        GuidTest.prototype.constructor = GuidTest;
	        
	        //Override the test Setup method
	        //Setup: Common set of functions that are performed just before each test method is called
	        GuidTest.prototype.SetUp = function()
	        { 
		        this.StartTime = new Date();
	        }

            //Override the test TearDown method
            //TearDown: Common set of functions that are performed after each test method is run
	        GuidTest.prototype.TearDown = function()
	        {
		        var elapsedTime = new Date().getTime() - this.StartTime.getTime();
		        Console.Out.Write("Finished in " + (elapsedTime / 1000 / 60) + "ms || " + (elapsedTime / 1000) + "s");
	        }
	        
	        //Create the test fixture instance
	        var guidTestFixture = new GuidTest();

	        //Create a test case
	        function TestGuidAlgorithm()
	        {
		        Console.Out.Write("Test guid algorithm...");

		        for(var i = 0; i< 3; i++)
		        {
			        var currentUid = guidComponent.GetNew();
			        Assert.That( currentUid, new Is.EqualTo("o_" + i) );
		        }
    			
		        Console.Out.Write("done!");
	        }
	        //Add test case to test fixture
	        guidTestFixture.AddTest(TestGuidAlgorithm);
	        
	        //Create another test case
	        function TestDifferentGuids()
	        {
		        Console.Out.Write("Test different guids algorithm...");

		        for(var i = 0; i< 3; i++)
		        {
			        var currentUid = guidComponent.GetNew();
			        //Assert.That( currentUid, new Is.NotEqualTo("o_" + i) );
	                //Force guid algorithm to fail (just for illustration, you don't really want your tests to fail right?)
			        Assert.That( currentUid, new Is.EqualTo("o_" + i) );
		        }
    			
		        Console.Out.Write("done!");
	        }
	        //Add test case to test fixture
	        guidTestFixture.AddTest(TestDifferentGuids);
	        
   	        //Add test fixture to unit.js engine
   	        UnitJs.AddTextFixture(guidTestFixture);
   	        //Run all test fixtures
   	        UnitJs.Run();
        </script>
    </body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Roberto Colnaghi
Software Developer
United States United States
I'm a passionate developer and videogame player.
Been in touch with Objective-C, Javascript, C#, C, Guild Wars 2, Tera and many more.
 
Javascript is one of my favorite languages.
Follow on   Twitter   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 15 Dec 2007
Article Copyright 2007 by Roberto Colnaghi
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid