Skip to content
cbaxter edited this page Dec 29, 2011 · 1 revision

TestScript Class

The TestScript class is the core of JSTest. The TestScript class wraps a CScript command and is the test runner for each JavaScript unit test.

Public Constructors

TestScript exposes two public constructors. The default public constructor sets a script timeout of ten seconds. Alternatively, you may use the overloaded constructor to specifcy a custom timeout for any given JavaScript unit test.

  public TestScript();                 // Defaults script timeout to 10 seconds
  public TestScript(TimeSpan timeout)  // Script timeout must be between 0 (infinite) and 32,767 seconds in length

Note: An ArgumentOutOfRangeException exception will be thrown if the specified TimeSpan is not between 0 and 32,767 seconds in duration.

Examples

  private readonly TestScript _testScript = new TestScript();
  private readonly TestScript _testScript = new TestScript(TimeSpan.FromMinutes(2));

AppendFile Method

AppendFile will append any required JavaScript file to the TestScript. Typically, this will be the JavaScript file under test, or common/shared JavaScript required by the file under test. AppendFile does not immediately validate that the file referenced is actually JavaScript; thus if a non-JavaScript file is referenced, a ScriptException will be thrown when you attempt to run a given unit test.

  public void AppendFile(String fileName);  // Renders a WSH script include element

Note: A FileNotFoundException exception will be thrown if the file does not exist.

Examples

  _testScript.AppendFile(@"..\..\Scripts\dateExtensions.js");
  _testScript.AppendFile(@"D:\SourceCode\JSTest.NET\Example.Tests\Scripts\dateExtensions.js");

AppendBlock Method

AppendBlock will append any required JavaScript code block to the TestScript. Typically, this will be embedded JavaScript libraries or common/shared JavaScript setup required by the file under test. AppendFile does not immediately validate that the code referenced is actually JavaScript; thus if a non-JavaScript file is referenced, a ScriptException will be thrown when you attempt to run a given unit test.

  public void AppendBlock(String scriptBlock);         // Renders a WSH script element
  public void AppendBlock(ScriptBlock scriptElement);  // Renders a WSH script element

Note: An ArgumentException exception will be thrown if the script block is null, empty or white-space only.

Examples

  _testScript.AppendBlock(new JsHamcrestLibrary());
  _testScript.AppendBlock(@"var cookieContainer = new CookieContainer(document);");
  _testScript.AppendBlock(String.Format("var x = {0}", JsonConverter.Serialize(new { id = 1, value = "one" }));
  _testScript.AppendBlock(new EmbeddedScriptBlock(typeof(SomeType).Assembly, "full.namespace.resource.name"));

Several commonly used JavaScript mock and assertion libraries have been embedded in JSTest for conveniance. Specifiaclly, the following five JavaScript libraries are included an may be referenced if desired:

  • JsHamcrest
    • Appended to TestScript instance via _testScript.AppendBlock(new JsHamcrestLibrary());
  • JsMockito
    • Appended to TestScript instance via _testScript.AppendBlock(new JsMockitoLibrary());
  • JsMock
    • Appended to TestScript instance via _testScript.AppendBlock(new JsMockLibrary());
  • JSON2
    • Included by default; no special action required.

RunTest Method

RunTest will invoke the specified JavaScript unit test with access to only those script blocks/includes appended to TestScript. Run test will return a JSON string containing the return value from the test block if specified.

  public String RunTest(String scriptBlock);
  public String RunTest(String scriptBlock, Boolean forceDebuggerPromptIfAttached);  

Note: The parameter forceDebuggerPromptIfAttached will default to true. Only override forceDebuggerPromptIfAttached if you will be running multiple test cases through a single method (i.e., xUnit theory).

Examples

  _testScript.RunTest(@"
                        document.cookie = 'MyCookie=' + escape('Chocolate Chip') + '; expires=' + new Date().toUTCString();

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


  _testScript.RunTest(@"
                        document.cookie = 'MyCookie=' + escape('Chocolate Chip') + '; expires=' + new Date().toUTCString();

                        assert.equal('Chocolate Chip', cookieContainer.getCookie('MyCookie'));
                      ", false);
Clone this wiki locally