Tagged: javascript RSS Toggle Comment Threads | Keyboard Shortcuts

  • John 1:56 pm on August 25, 2010 Permalink | Reply
    Tags: javascript   

    Javascript string.format 

    String.prototype.format = function () {
        formatted = this;
        for (i = 0; i < arguments.length; i++) {
            formatted = formatted.replace("{" + i + "}", arguments[i]);
        }
        return formatted;
    }
    
     
  • John 10:06 pm on September 26, 2009 Permalink | Reply
    Tags: , , javascript, , ,   

    jqRunner Snapshot – Running Javascript Unit test with NUnit 

    One of the nice things about unit tests is that you can use continuous integration so that you don’t have to run them yourself. At work we use CruiseControl.NET to automate our builds and run our unit tests. Now as a number of our projects are web based we use a fair amount of javascript, mainly jQuery, though in-house plugins. In order to test these we use jqunit, this works great and allows us to use TDD when writing javascript. However as development goes on the unit tests get forgotten about as they are not automatically ran.

    In order so solve this I decided to experiment to find a method of getting cruise control to run our jqunit tests for me. To run the tests I looked at wrapping the jqunit tests with nunit tests.

    jqRunner

    jqRunner is designed so that you can us your existing jqunit tests in their existing location without changing them. jqRunner required that all the scripts that are needed to run are registered. The full path to the file is required. jqRunner then executes the tests and returns the results, which are then parsed by nunit as tests using the TestCaseSource attribute.

    var sampleTestCase = new jqUnit.TestCase('Sample Test Case', function() {
        /*setup*/
        // this.yep(1);
    }, function() {
        /*teardown*/
        // this.ok(1)
    });
    sampleTestCase.test('Sample Test 1', function() {
        this.ok(1);
    });
    sampleTestCase.test('Sample Test 2', function() {
        this.ok(0);
    });
    
    using System;
    using System.Collections.Generic;
    using NUnit.Framework;
    using jqRunner;
    using System.IO;
    using System.Reflection;
    
    namespace jqRunner.Tests
    {
        [TestFixture,RequiresSTA]
        public class TestCaseSourceTests
        {
            [Test, TestCaseSource("GetTestResults"), RequiresSTA]
            public void CheckTest(ITestResult result)
            {
                Assert.IsTrue(result.Pass, result.Name);
            }
    
            private static ITestResult[] GetTestResults()
            {
                string jsFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase) + @"\jqUnitTests\SampleUnitTest.js";
                TestBed target = new TestBed();
                target.RegisterScript(jsFile);
                return target.Execute().ToArray();
            }
        }
    }
    

    This is an initial development snapshot and is bound to have plenty of problems.

    Download
    http://static.yeticode.co.uk/blog/downloads/jqRunner-snapshot.zip

    Related Reading

     
  • John 9:10 am on June 12, 2009 Permalink | Reply
    Tags: javascript, , ,   

    Unit testing jQuery plugins with jqunit 

    Whilst creating a plugin for jquery, that would provide auto formatting for a textbox for numeric values, I was looking for a way to run the tests for confirm its functionality. After getting sick of running the tests manually the penny dropped that I could unit test the plugins functionality. After a little searching I came across jqunit that seems to fit the bill perfectly.

    The project is hosted over at google code. The tests them self run from a html file but the a template is provided with the jqunit library. The tests are simple to create and follow the jquery syntax style. Below is a smple test case with unit test

    var numericTestCase = new jqUnit.TestCase('jQuery.numeric - decimal',function(){
     /*setup*/
    
      },function(){
    
        /*teardown*/
    
    });
    numericTestCase.test('T1: Null Value ',function(){
        var txtInput = $("
    ");
        txtInput.numeric(true);
        txtInput.blur();
    
        jqUnit.equals(txtInput.val(),"0.00");
    });
    

    Tests can quickly be written to test the functionality of your code. With the testing being able to run form a single location it provides a quick an easy way to check your plugins work across multiple browsers, that is, as long as you have written the correct tests.

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel