Enhance JS JavaScript Unit Testing Framework

Mocks And Stubs

Enhance JS has built-in support for mocks and stubs. You can set up expectations and supply predictable return values.

You can simply create a mock and then add an expectation and then check that the expectation has been met. Due to language restrictions with JavaScript, you need to specify all methods and properties that are called even if you don't want to specifically mock them.

Here is an example:

Examples = (function() {
    return {
        mockExample: function (validator, value) {
            if (validator.validate(value)) {
                return validator.sanitize(value);
            }
            return validator.getErrorMessage(value);
        }
    };
}());

ExamplesTest = (function() {
    return {
        mockExample: function () {
            var testValue = 'example input';
            var expectedOutput = 'output';
            var mockValidator = MockRepository.generateMock().methods('validate', 'sanitize');
            mockValidator.expect().method('validate').withArguments(testValue).returns(true).times(1);
            mockValidator.expect().method('sanitize').withArguments(testValue).returns(expectedOutput).times(1);

            var result = Examples.mockExample(mockValidator, testValue);

            Assert.areIdentical(expectedOutput, result);
            mockValidator.verifyExpectations();
        }
    };
})();

Enhance.discoverTests(ExamplesTest).runTests();

In this example, we create a mock that will contain functions or properties 'validate' and 'sanitize'. If we didn't want to ensure that these methods were called, or the result values weren't important we would not need the next two lines - we would be treating the mock as a stub (i.e. it is not being used to validate anything, just as a stand in).

To use the mock to verify calls, set up expectations for the calls and call "verifyExpectations" at the end of your test.

Mocking jQuery

There is a special helper that makes it easier to mock jQuery. Instead of calling generateMock, use the generateJqueryMock method:

var mock = MockRepository.generateJqueryMock('$', '#test').methods('attr');
mock.expect().method('attr').withArguments('alt', description).returns(mock).times(1);

When you call generateJqueryMoc, you specify your jQuery alias (normally '$' or 'jQuery') and the selector that it will look for when giving back a mock, for example '#test' or '.someClass'.

Additionally, normal jQuery behaviour is that the jQuery object is returned from each call, so when you add expectations, add 'returns(mock)' to reproduce this behaviour.

If you need to mock many selectors, just call generateJqueryMock for each one:

var idTestMock = MockRepository.generateJqueryMock('$', '#test').methods('attr');
// jQuery.attr() returns the jQuery object
idTestMock.expect().method('attr').withArguments('alt', description).returns(idTestMock).times(1);
// jQuery.title() returns the jQuery object
idTestMock.expect().method('attr').withArguments('title', description).returns(idTestMock).times(1);

var classListMock = MockRepository.generateJqueryMock('$', '.list').methods('width');
// jQuery.width() returns a number
classListMock.expect().method('width').returns(321).times(1);