JS/CoffeeScript Test Reference

  • Sep 11, 2013

Test

The Test object provides the testing functionality needed to validate a kata's requirements. It is a frozen object and cannot be modified.

Pass/Fail methods

Test.expect(passed, msg)

Core assertion method that all other methods build off of. msg argument is optional. If it is not provided then a generic message will be used. Best practice is to provide your own message.

Pass/Fail status will be written to the output stream.

Test.assertEquals(actual, expected, msg)

Checks that the actual value equals (===) the expected value. A useful message will be displayed
for both pass and fail outcomes. The msg argument is optional. If given it will be displayed in addition to the typical message used.

Test.assertNotEquals(actual, unexpected, msg)

Checks that the actual value does not equal (!==) the unexpected value. A useful message will be displayed for both pass and fail outcomes. The msg argument is optional. If given it will be displayed in addition to the typical message used.

Test.assertSimilar(actual, expected, msg)

Checks that the actual value equals (===) the expected value. Test.inspect is used to wrap the values being tested, allowing for similar values to be considered the same. A useful message will be displayed for both pass and fail outcomes. The msg argument is optional. If given it will be displayed in addition to the typical message used.

Test.assertNotSimilar(actual, unexpected, msg)

Checks that the actual value does not equal (!==) the unexpected value. Test.inspect is used to wrap the values being tested, allowing for similar values to be considered the same. A useful message will be displayed for both pass and fail outcomes. The msg argument is optional. If given it will be displayed in addition to the typical message used.

Test.expectError(msg, fn)

Useful for testing that an error was expected to happen. msg is optional but best practice is to provide one.

Test.expectNoError(msg, fn)

Useful for testing that an error was not expected to happen. msg is optional but best practice is to provide one.

Spec Methods:

Test.describe(msg, fn)

Top level method for describing/grouping a set of tests. Globally aliased as "describe".

describe("Foo", function(){

});

// or

Test.describe("Foo", function(){

});

Test.it(msg, fn)

Used in conjunction with describe to group related sets of tests in a spec. Globally aliased as "it".

describe("Foo", function(){
    it ("should be defined", function(){
        Test.expect(this.Foo, "Foo is not defined")
    });
});

Test.before(fn)

Any callbacks sent to this method will be called before each it spec is ran.

// this is a contrived example
describe("Foo", function(){
    var a = 0;

    // called before each spec is ran
    before(function(){
        a++;
    });

    it ("should should do something", function(){
        Test.assertEquals(a, 1) 
    });

    it ("should do something else", function(){
        Test.assertEquals(a, 2)
    });
});

Helper Methods:

Test.callCount(methodName) → Integer

Returns the number of times a Test method has been called. Useful when creating kata that need to test the usage of Test methods. Also useful when using Test helper methods are disallowed within the kata solution. You can simply test if any disallowed calls have been made.

Test.inspect(object) → String

Returns a string representation of the object. For simple objects toString() will be used, for complex objects a JSON representation will be given.

Test.randomize(array) → Array

Test.randomNumber() → Integer

Test.randomToken() → String

Test.sample(array) → object

Discuss:

Loading comments...