Skip to content
bard edited this page Sep 13, 2010 · 1 revision

Please note: this is incomplete and work in progress!

Predefined assertions

  • equals(x, y)
  • notEquals(x, y)
  • isTrue(x)
  • isDefined(x)
  • isUndefined(x)
  • isFalse(x)
  • isNull(x)
  • raises(exception, code, context)
  • matches(pattern, string)

TestCase

constructor(title, opts)

Invocation:


    var case = new TestCase('Widget tests');
    var case = new TestCase('Widget tests', {runStrategy: 'async'});

Use async run strategy when test cases mustn’t be run immediately
after test setup, for example when during setup a document is
loaded into the browser and the browser will signal when the
document has finished loading through a callback.

Note: code inside tests will still run sequentially. In some
cases, e.g. testing interfaces, this means you will do something to
affect the interface and then test the effect, before the interface
thread has had a chance to apply your request. Control of flow
inside tests is work in progress.

Alias:


    var spec = new Specification();

setTests(hash)

Define test cases, optionally with setup and teardown.


    var case = new TestCase();
    case.tests = {
        setUp: function() {
            this.plusFactor = 4;
        },

        testOperation: function() {
            assert.equals(8, 2+2+this.plusFactor);
        },

        tearDown: function() {
            // release resources if necessary
        }
    }

Every test is run in a context created ex-novo and accessible from
the test itself via the ‘this’ identifier.

Aliases: setTests(), ‘stateThat’. ‘setUp’ is also aliased to
‘given’. ‘stateThat’ and ‘given’ allow a more Behaviour-Driven
Development style.


    var spec = new Specification();
    spec.stateThat = {
        given: function() {
            this.plusFactor = 4;
        },

        'Adding two and two and plus factor yields eight': function() {
            assert.equals(8, 2+2+this.plusFactor);
        },

        tearDown: function() {
            // release resources if necessary
        }
    }

run()

Runs tests with strategy defined at construction time.


   var case = new TestCase();
   case.tests = { ... };
   case.run();

setUp(fn)

Alternative style for defining setup.

test(desc, code)

Alternative style for defining tests. Can be called multiple
times.

tearDown(fn)

Alternative style for defining teardown.

verify()

BDD-style alias for run().


   var spec = new Specification();
   spec.stateThat = { ... };
   spec.verify();

given(fn)

BDD-alias for setUp().

states(desc, fn)

BDD-style alias for test().

Clone this wiki locally