QUnit Tests

The package Saltarelle.QUnit enables support for generating QUnit tests. All classes for this are in the namespace QUnit. Of course, if using this feature, the official QUnit library must be included on the page at runtime.

To specify that a class contains tests, decorate it with a [TestFixtureAttribute]. This enables the [TestAttribute] to be used on instance methods for the types. Any method decorated with one of those attributes will not be compiled as a regular method, but rather as a test() or asyncTest() call in a special instance method called runTests. In those attributes you can also specify a test category (equivalent to a QUnit module), as well as the test name and the expected assertion count.

Example

This C# source code:

[TestFixture]
public class MyFixture {
	private void UtilityMethod() {
	}
	[Test]
	public void SomeTest() {
		Assert.AreEqual("1", 1.ToString());
	}
	[Test(Category = "My category")]
	public void SomeOtherTest() {
		Assert.AreEqual("1", 1.ToString());
	}
} 

will be translated to this JavaScript:

MyFixture = function() {
};
MyFixture.prototype = {
	$utilityMethod: function() {
	},
	runTests: function() {
		test('SomeTest', Function.mkdel(function() {
			deepEqual('1', 1.toString());
		}, this));
		module('My category');
		test('SomeOtherTest', Function.mkdel(function() {
			deepEqual('1', 1.toString());
		}, this));
	}
};