Skip to content

Extended xUnit Integration

cbaxter edited this page Dec 31, 2011 · 1 revision

Extended xUnit Integration

xUnit has the ability to support paramizerized tests via the use of the TheoryAttribute and DataAttributes. In order to better facilitate pushing all test JavaScript in to JS files, an extension to JSTest has been created to support cleaner tests.

JavaScriptTestBase class

The JavaScriptTestBase class provides a very basic test foundation for writing JavaScript tests.

Constructors

Two protected constructors have been defined:

protected JavaScriptTestBase()

The default constructor is the preferred method of instantiating a JavaScript test class. IncludeDefaultBreakpint is set to false to ensure that if you choose to run your JavaScript tests with a debugger attached you will not be prompted to debug each individual test contained within the test file(s).

protected JavaScriptTestBase(Boolean includeDefaultBreakpoint)

An overloaded constructor has also been provided to allow you to manually select the IncludeDefaultBreakpint behavior.

Methods

The JavaScriptTestBase class has a single protected method RunTest that may be called.

protected String RunTest(String context, String action)

The context parameter is typically the source file name for the current test being executed. The action parameter is the function name that represents the test to be run. See below for sample usage.

Sample xUnit Test Fixture

  public class UsingCookieContainer : JavaScriptTestBase
  {
    public UsingCookieContainer()
    {
      // Append required JavaScript libraries.
      Script.AppendBlock(new JsAssertLibrary());

      // Append required JavaScript Files.
      Script.AppendFile(@"..\..\Scripts\dateExtensions.js");
      Script.AppendFile(@"..\..\Scripts\cookieContainer.js");
    }

    [JavaScriptTestSuite]
    [JavaScriptTestFile(@"..\..\Style1\whenGettingCookies.js")]
    [JavaScriptTestFile(@"..\..\Style1\whenSettingCookies.js")]
    public void Test(String context, String action, String fileName)
    {
      // Append JavaScript 'Fact' File.
      Script.AppendFile(fileName);

      // Verify 'Fact'.
      RunTest(context, action);
    }

Sample JavaScript Test File

  var document = {};
  var cookieContainer = new CookieContainer(document);

  function returnEmptyStringIfCookiesNotSet() {
    document.cookie = '';

    assert.equal('', cookieContainer.getCookie('MyCookie'));
  }

  function returnCookieValueIfSingleCookieDefined() {
    document.cookie = 'MyCookie=' + escape('Chocolate Chip') + '; expires=' + new Date().toUTCString();

    assert.equal('Chocolate Chip', cookieContainer.getCookie('MyCookie'));
  }

  function returnLastCookieValueIfMultipleCookiesDefined() {
    var cookie1 = 'MyCookie1=' + escape('Chocolate Chip') + '; expires=' + new Date().toUTCString();
    var cookie2 = 'MyCookie2=' + escape('Peanut Butter') + '; expires=' + new Date().toUTCString();

    document.cookie = cookie1 + '; ' + cookie2;

    assert.equal('Peanut Butter', cookieContainer.getCookie('MyCookie2'));
  }

  function returnCookieValueIfLikeNamedCookiesDefined() {
    var cookie1 = 'MyCookie=' + escape('Chocolate Chip') + '; expires=' + new Date().toUTCString();
    var cookie2 = 'AlsoMyCookie=' + escape('Peanut Butter') + '; expires=' + new Date().toUTCString();

    document.cookie = cookie1 + '; ' + cookie2;

    assert.equal('Chocolate Chip', cookieContainer.getCookie('MyCookie'));
  }

JavaScript Test Function Naming

JSTest by default will assume any named JavaScript function in a specified JavaScript file is a test function. If this behaviour does not suite your needs, you can specify a custom matching pattern on the JavaScriptFileAttribute as shown below:

  [JavaScriptTestFile(@"..\..\Style1\whenSettingCookies.js", "test_[A-Za-z]+")]

Please note that the pattern is only to match the function name; the JavaScriptTestAttribute will wrap the naming pattern with the following regex expression ^\s*function\s+(?<fact><PATTERN>)\s*\ (\s*\)\s*\{?\s*$.

Clone this wiki locally